Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Last remnant of PromisChain gone, hooray!

  • Loading branch information...
commit a1c315ba4a186ad2246d09571245ab6009e5adda 1 parent 45b85b2
@isaacs isaacs authored
Showing with 59 additions and 66 deletions.
  1. +59 −27 lib/adduser.js
  2. +0 −39 lib/utils/promise-chain.js
View
86 lib/adduser.js
@@ -6,7 +6,6 @@ var ini = require("./utils/ini.js")
, npm = require("./npm.js")
, registry = npm.registry
, read = require("read")
- , promiseChain = require("./utils/promise-chain.js")
, crypto
try {
@@ -19,35 +18,68 @@ function adduser (args, cb) {
if (!crypto) return cb(new Error(
"You must compile node with ssl support to use the adduser feature"))
- var u = { u : npm.config.get("username")
+ var c = { u : npm.config.get("username")
, p : npm.config.get("_password")
, e : npm.config.get("email")
}
, changed = false
+ , u = {}
+ , fns = [readUsername, readPassword, readEmail, save]
- promiseChain(cb)
- (read, [{prompt: "Username: ", default: u.u}], function (un) {
- changed = u.u !== un
- u.u = un
- })
- (function (cb) {
- if (u.p && !changed) return cb(null, u.p)
- read({prompt: "Password: ", default: u.p, silent: true}, cb)
- }, [], function (pw) { u.p = pw })
- (read, [{prompt: "Email: ", default: u.e}], function (em) { u.e = em })
- (function (cb) {
- if (changed) npm.config.del("_auth")
- registry.adduser(u.u, u.p, u.e, function (er) {
- if (er) return cb(er)
- registry.username = u.u
- registry.password = u.p
- registry.email = u.e
- ini.set("username", u.u, "user")
- ini.set("_password", u.p, "user")
- ini.set("email", u.e, "user")
- log.info("adduser", "Authorized user %s", u.u)
- ini.save("user", cb)
- })
- })
- ()
+ loop()
+ function loop (er) {
+ if (er) return cb(er)
+ var fn = fns.shift()
+ if (fn) return fn(c, u, loop)
+ cb()
+ }
+}
+
+function readUsername (c, u, cb) {
+ read({prompt: "Username: ", default: c.u}, function (er, un) {
+ c.changed = c.u !== un
+ u.u = un
+ cb(er)
+ })
+}
+
+function readPassword (c, u, cb) {
+ if (!c.changed) {
+ u.p = c.p
+ return cb()
+ }
+ read({prompt: "Password: ", silent: true}, function (er, pw) {
+ u.p = pw
+ cb(er)
+ })
+}
+
+function readEmail (c, u, cb) {
+ read({prompt: "Email: ", default: c.e}, function (er, em) {
+ u.e = em
+ cb(er)
+ })
+}
+
+function save (c, u, cb) {
+ if (c.changed) {
+ delete registry.auth
+ delete registry.username
+ delete registry.password
+ registry.username = u.u
+ registry.password = u.p
+ }
+
+ // save existing configs, but yank off for this PUT
+ registry.adduser(u.u, u.p, u.e, function (er) {
+ if (er) return cb(er)
+ registry.username = u.u
+ registry.password = u.p
+ registry.email = u.e
+ ini.set("username", u.u, "user")
+ ini.set("_password", u.p, "user")
+ ini.set("email", u.e, "user")
+ log.info("adduser", "Authorized user %s", u.u)
+ ini.save("user", cb)
+ })
}
View
39 lib/utils/promise-chain.js
@@ -1,39 +0,0 @@
-
-module.exports = promiseChain
-
-// usage:
-//
-// promiseChain(cb) <-- this is the callback for eventual success or error
-// ( fn, [arg, arg, arg], function (a,b,c) { success(a,b,c) })
-// ( fn2, [args] )
-// () <-- this kicks it off.
-//
-// promiseChain.call(someObj, cb) <-- bind this-context for all functions
-
-function promiseChain (cb) {
- var steps = []
- , vals = []
- , context = this
- function go () {
- var step = steps.shift()
- if (!step) return cb()
- try { step[0].apply(context, step[1]) }
- catch (ex) { cb(ex) }
- }
- return function pc (fn, args, success) {
- if (arguments.length === 0) return go()
- // add the step
- steps.push
- ( [ fn
- , (args || []).concat([ function (er) {
- if (er) return cb(er)
- var a = Array.prototype.slice.call(arguments, 1)
- try { success && success.apply(context, a) }
- catch (ex) { return cb(ex) }
- go()
- }])
- ]
- )
- return pc
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.