1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
'use strict'
const log = require('npmlog')
const npm = require('./npm.js')
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')
const replaceInfo = require('./utils/replace-info.js')
const authTypes = {
legacy: require('./auth/legacy.js'),
oauth: require('./auth/oauth.js'),
saml: require('./auth/saml.js'),
sso: require('./auth/sso.js'),
}
const usage = usageUtil(
'adduser',
'npm adduser [--registry=url] [--scope=@orgname] [--always-auth]'
)
const completion = require('./utils/completion/none.js')
const cmd = (args, cb) => adduser(args).then(() => cb()).catch(cb)
const getRegistry = ({ scope, registry }) => {
if (scope) {
const scopedRegistry = npm.config.get(`${scope}:registry`)
const cliRegistry = npm.config.get('registry', 'cli')
if (scopedRegistry && !cliRegistry)
return scopedRegistry
}
return registry
}
const getAuthType = ({ authType }) => {
const type = authTypes[authType]
if (!type)
throw new Error('no such auth module')
return type
}
const updateConfig = async ({ newCreds, registry, scope }) => {
npm.config.delete('_token', 'user') // prevent legacy pollution
if (scope)
npm.config.set(scope + ':registry', registry, 'user')
npm.config.setCredentialsByURI(registry, newCreds)
await npm.config.save('user')
}
const adduser = async (args) => {
const { scope } = npm.flatOptions
const registry = getRegistry(npm.flatOptions)
const auth = getAuthType(npm.flatOptions)
const creds = npm.config.getCredentialsByURI(registry)
log.disableProgress()
log.notice('', `Log in on ${replaceInfo(registry)}`)
const { message, newCreds } = await auth({
creds,
registry,
scope,
})
await updateConfig({
newCreds,
registry,
scope,
})
output(message)
}
module.exports = Object.assign(cmd, { completion, usage })
|