2024-04-30 23:53:22 -07:00
|
|
|
const { log, output } = require('proc-log')
|
2024-04-07 14:36:14 -07:00
|
|
|
const { redactLog: replaceInfo } = require('@npmcli/redact')
|
2022-12-06 22:18:33 -05:00
|
|
|
const auth = require('../utils/auth.js')
|
2024-04-30 23:53:22 -07:00
|
|
|
const BaseCommand = require('../base-cmd.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class AddUser extends BaseCommand {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Add a registry user account'
|
|
|
|
static name = 'adduser'
|
|
|
|
static params = [
|
|
|
|
'registry',
|
|
|
|
'scope',
|
2022-06-13 23:04:21 -07:00
|
|
|
'auth-type',
|
2021-11-18 20:58:02 +00:00
|
|
|
]
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
async exec () {
|
2022-12-06 22:18:33 -05:00
|
|
|
const scope = this.npm.config.get('scope')
|
|
|
|
let registry = this.npm.config.get('registry')
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
if (scope) {
|
|
|
|
const scopedRegistry = this.npm.config.get(`${scope}:registry`)
|
|
|
|
const cliRegistry = this.npm.config.get('registry', 'cli')
|
2021-11-18 20:58:02 +00:00
|
|
|
if (scopedRegistry && !cliRegistry) {
|
2022-12-06 22:18:33 -05:00
|
|
|
registry = scopedRegistry
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
const creds = this.npm.config.getCredentialsByURI(registry)
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
log.notice('', `Log in on ${replaceInfo(registry)}`)
|
2020-11-13 15:30:43 -05:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
const { message, newCreds } = await auth.adduser(this.npm, {
|
|
|
|
...this.npm.flatOptions,
|
|
|
|
creds,
|
|
|
|
registry,
|
|
|
|
})
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
this.npm.config.delete('_token', 'user') // prevent legacy pollution
|
2021-06-10 20:49:57 +00:00
|
|
|
this.npm.config.setCredentialsByURI(registry, newCreds)
|
2022-12-06 22:18:33 -05:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (scope) {
|
2021-03-04 17:40:28 -05:00
|
|
|
this.npm.config.set(scope + ':registry', registry, 'user')
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2022-12-06 22:18:33 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
await this.npm.config.save('user')
|
2022-12-06 22:18:33 -05:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
output.standard(message)
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = AddUser
|