1const log = require('../utils/log-shim.js')
2const replaceInfo = require('../utils/replace-info.js')
3const auth = require('../utils/auth.js')
4
5const BaseCommand = require('../base-command.js')
6
7class AddUser extends BaseCommand {
8  static description = 'Add a registry user account'
9  static name = 'adduser'
10  static params = [
11    'registry',
12    'scope',
13    'auth-type',
14  ]
15
16  async exec (args) {
17    const scope = this.npm.config.get('scope')
18    let registry = this.npm.config.get('registry')
19
20    if (scope) {
21      const scopedRegistry = this.npm.config.get(`${scope}:registry`)
22      const cliRegistry = this.npm.config.get('registry', 'cli')
23      if (scopedRegistry && !cliRegistry) {
24        registry = scopedRegistry
25      }
26    }
27
28    const creds = this.npm.config.getCredentialsByURI(registry)
29
30    log.disableProgress()
31    log.notice('', `Log in on ${replaceInfo(registry)}`)
32
33    const { message, newCreds } = await auth.adduser(this.npm, {
34      ...this.npm.flatOptions,
35      creds,
36      registry,
37    })
38
39    this.npm.config.delete('_token', 'user') // prevent legacy pollution
40    this.npm.config.setCredentialsByURI(registry, newCreds)
41
42    if (scope) {
43      this.npm.config.set(scope + ':registry', registry, 'user')
44    }
45
46    await this.npm.config.save('user')
47
48    this.npm.output(message)
49  }
50}
51module.exports = AddUser
52