diff options
Diffstat (limited to 'lib/adduser.js')
-rw-r--r-- | lib/adduser.js | 92 |
1 files changed, 47 insertions, 45 deletions
diff --git a/lib/adduser.js b/lib/adduser.js index 79babe02c..cd4db2b5d 100644 --- a/lib/adduser.js +++ b/lib/adduser.js @@ -1,29 +1,32 @@ - module.exports = adduser -var log = require("npmlog") - , npm = require("./npm.js") - , read = require("read") - , userValidate = require("npm-user-validate") - , crypto +var log = require('npmlog') +var npm = require('./npm.js') +var read = require('read') +var userValidate = require('npm-user-validate') +var crypto try { - crypto = process.binding("crypto") && require("crypto") + crypto = process.binding('crypto') && require('crypto') } catch (ex) {} -adduser.usage = "npm adduser [--registry=url] [--scope=@orgname] [--always-auth]" +adduser.usage = 'npm adduser [--registry=url] [--scope=@orgname] [--always-auth]' function adduser (args, cb) { - if (!crypto) return cb(new Error( - "You must compile node with ssl support to use the adduser feature")) + if (!crypto) { + return cb(new Error( + 'You must compile node with ssl support to use the adduser feature' + )) + } - var creds = npm.config.getCredentialsByURI(npm.config.get("registry")) - var c = { u : creds.username || "" - , p : creds.password || "" - , e : creds.email || "" - } - , u = {} - , fns = [readUsername, readPassword, readEmail, save] + var creds = npm.config.getCredentialsByURI(npm.config.get('registry')) + var c = { + u: creds.username || '', + p: creds.password || '', + e: creds.email || '' + } + var u = {} + var fns = [readUsername, readPassword, readEmail, save] loop() function loop (er) { @@ -36,9 +39,9 @@ function adduser (args, cb) { function readUsername (c, u, cb) { var v = userValidate.username - read({prompt: "Username: ", default: c.u || ""}, function (er, un) { + read({prompt: 'Username: ', default: c.u || ''}, function (er, un) { if (er) { - return cb(er.message === "cancelled" ? er.message : er) + return cb(er.message === 'cancelled' ? er.message : er) } // make sure it's valid. we have to do this here, because @@ -67,17 +70,17 @@ function readPassword (c, u, cb) { var prompt if (c.p && !c.changed) { - prompt = "Password: (or leave unchanged) " + prompt = 'Password: (or leave unchanged) ' } else { - prompt = "Password: " + prompt = 'Password: ' } read({prompt: prompt, silent: true}, function (er, pw) { if (er) { - return cb(er.message === "cancelled" ? er.message : er) + return cb(er.message === 'cancelled' ? er.message : er) } - if (!c.changed && pw === "") { + if (!c.changed && pw === '') { // when the username was not changed, // empty response means "use the old value" pw = c.p @@ -101,10 +104,10 @@ function readPassword (c, u, cb) { function readEmail (c, u, cb) { var v = userValidate.email - var r = { prompt: "Email: (this IS public) ", default: c.e || "" } + var r = { prompt: 'Email: (this IS public) ', default: c.e || '' } read(r, function (er, em) { if (er) { - return cb(er.message === "cancelled" ? er.message : er) + return cb(er.message === 'cancelled' ? er.message : er) } if (!em) { @@ -124,48 +127,47 @@ function readEmail (c, u, cb) { function save (c, u, cb) { // save existing configs, but yank off for this PUT - var uri = npm.config.get("registry") - var scope = npm.config.get("scope") + var uri = npm.config.get('registry') + var scope = npm.config.get('scope') // there may be a saved scope and no --registry (for login) if (scope) { - if (scope.charAt(0) !== "@") scope = "@" + scope + if (scope.charAt(0) !== '@') scope = '@' + scope - var scopedRegistry = npm.config.get(scope + ":registry") - var cliRegistry = npm.config.get("registry", "cli") + var scopedRegistry = npm.config.get(scope + ':registry') + var cliRegistry = npm.config.get('registry', 'cli') if (scopedRegistry && !cliRegistry) uri = scopedRegistry } var params = { - auth : { - username : u.u, - password : u.p, - email : u.e + auth: { + username: u.u, + password: u.p, + email: u.e } } npm.registry.adduser(uri, params, function (er, doc) { if (er) return cb(er) // don't want this polluting the configuration - npm.config.del("_token", "user") + npm.config.del('_token', 'user') - if (scope) npm.config.set(scope + ":registry", uri, "user") + if (scope) npm.config.set(scope + ':registry', uri, 'user') if (doc && doc.token) { npm.config.setCredentialsByURI(uri, { - token : doc.token + token: doc.token }) - } - else { + } else { npm.config.setCredentialsByURI(uri, { - username : u.u, - password : u.p, - email : u.e, - alwaysAuth : npm.config.get("always-auth") + username: u.u, + password: u.p, + email: u.e, + alwaysAuth: npm.config.get('always-auth') }) } - log.info("adduser", "Authorized user %s", u.u) - npm.config.save("user", cb) + log.info('adduser', 'Authorized user %s', u.u) + npm.config.save('user', cb) }) } |