summaryrefslogtreecommitdiff
path: root/deps/npm/lib/adduser.js
blob: f0fc8d10edcba99ae850cfba42dba233669cfc4f (plain)
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

module.exports = adduser

var ini = require("./utils/ini.js")
  , log = require("npmlog")
  , npm = require("./npm.js")
  , registry = npm.registry
  , read = require("read")
  , promiseChain = require("./utils/promise-chain.js")
  , crypto

try {
  crypto = process.binding("crypto") && require("crypto")
} catch (ex) {}

adduser.usage = "npm adduser\nThen enter stuff at the prompts"

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")
          , p : npm.config.get("_password")
          , e : npm.config.get("email")
          }
    , changed = false

  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)
      })
    })
    ()
}