adduser.js 1.3 KB

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