11cb0ef41Sopenharmony_ciconst log = require('../utils/log-shim.js')
21cb0ef41Sopenharmony_ciconst replaceInfo = require('../utils/replace-info.js')
31cb0ef41Sopenharmony_ciconst auth = require('../utils/auth.js')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst BaseCommand = require('../base-command.js')
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciclass AddUser extends BaseCommand {
81cb0ef41Sopenharmony_ci  static description = 'Add a registry user account'
91cb0ef41Sopenharmony_ci  static name = 'adduser'
101cb0ef41Sopenharmony_ci  static params = [
111cb0ef41Sopenharmony_ci    'registry',
121cb0ef41Sopenharmony_ci    'scope',
131cb0ef41Sopenharmony_ci    'auth-type',
141cb0ef41Sopenharmony_ci  ]
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  async exec (args) {
171cb0ef41Sopenharmony_ci    const scope = this.npm.config.get('scope')
181cb0ef41Sopenharmony_ci    let registry = this.npm.config.get('registry')
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    if (scope) {
211cb0ef41Sopenharmony_ci      const scopedRegistry = this.npm.config.get(`${scope}:registry`)
221cb0ef41Sopenharmony_ci      const cliRegistry = this.npm.config.get('registry', 'cli')
231cb0ef41Sopenharmony_ci      if (scopedRegistry && !cliRegistry) {
241cb0ef41Sopenharmony_ci        registry = scopedRegistry
251cb0ef41Sopenharmony_ci      }
261cb0ef41Sopenharmony_ci    }
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    const creds = this.npm.config.getCredentialsByURI(registry)
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    log.disableProgress()
311cb0ef41Sopenharmony_ci    log.notice('', `Log in on ${replaceInfo(registry)}`)
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    const { message, newCreds } = await auth.adduser(this.npm, {
341cb0ef41Sopenharmony_ci      ...this.npm.flatOptions,
351cb0ef41Sopenharmony_ci      creds,
361cb0ef41Sopenharmony_ci      registry,
371cb0ef41Sopenharmony_ci    })
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    this.npm.config.delete('_token', 'user') // prevent legacy pollution
401cb0ef41Sopenharmony_ci    this.npm.config.setCredentialsByURI(registry, newCreds)
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    if (scope) {
431cb0ef41Sopenharmony_ci      this.npm.config.set(scope + ':registry', registry, 'user')
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    await this.npm.config.save('user')
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    this.npm.output(message)
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_cimodule.exports = AddUser
52