UNPKG

1.34 kBJavaScriptView Raw
1module.exports = adduser
2
3const log = require('npmlog')
4const npm = require('./npm.js')
5const usage = require('./utils/usage')
6let crypto
7
8try {
9 crypto = require('crypto')
10} catch (ex) {}
11
12adduser.usage = usage(
13 'adduser',
14 'npm adduser [--registry=url] [--scope=@orgname] [--auth-type=legacy] [--always-auth]'
15)
16
17function adduser (args, cb) {
18 if (!crypto) {
19 return cb(new Error(
20 'You must compile node with ssl support to use the adduser feature'
21 ))
22 }
23
24 let registry = npm.config.get('registry')
25 const scope = npm.config.get('scope')
26 const creds = npm.config.getCredentialsByURI(npm.config.get('registry'))
27
28 if (scope) {
29 const scopedRegistry = npm.config.get(scope + ':registry')
30 const cliRegistry = npm.config.get('registry', 'cli')
31 if (scopedRegistry && !cliRegistry) registry = scopedRegistry
32 }
33
34 log.disableProgress()
35
36 let auth
37 try {
38 auth = require('./auth/' + npm.config.get('auth-type'))
39 } catch (e) {
40 return cb(new Error('no such auth module'))
41 }
42 auth.login(creds, registry, scope, function (err, newCreds) {
43 if (err) return cb(err)
44
45 npm.config.del('_token', 'user') // prevent legacy pollution
46 if (scope) npm.config.set(scope + ':registry', registry, 'user')
47 npm.config.setCredentialsByURI(registry, newCreds)
48 npm.config.save('user', cb)
49 })
50}