1 |
|
2 | module.exports = adduser
|
3 |
|
4 | var log = require("npmlog")
|
5 | , npm = require("./npm.js")
|
6 | , registry = npm.registry
|
7 | , read = require("read")
|
8 | , userValidate = require("npm-user-validate")
|
9 | , crypto
|
10 |
|
11 | try {
|
12 | crypto = process.binding("crypto") && require("crypto")
|
13 | } catch (ex) {}
|
14 |
|
15 | adduser.usage = "npm adduser\nThen enter stuff at the prompts"
|
16 |
|
17 | function adduser (args, cb) {
|
18 | if (!crypto) return cb(new Error(
|
19 | "You must compile node with ssl support to use the adduser feature"))
|
20 |
|
21 | var c = { u : npm.config.get("username") || ""
|
22 | , p : npm.config.get("_password") || ""
|
23 | , e : npm.config.get("email") || ""
|
24 | }
|
25 | , changed = false
|
26 | , u = {}
|
27 | , fns = [readUsername, readPassword, readEmail, save]
|
28 |
|
29 | loop()
|
30 | function loop (er) {
|
31 | if (er) return cb(er)
|
32 | var fn = fns.shift()
|
33 | if (fn) return fn(c, u, loop)
|
34 | cb()
|
35 | }
|
36 | }
|
37 |
|
38 | function readUsername (c, u, cb) {
|
39 | var v = userValidate.username
|
40 | read({prompt: "Username: ", default: c.u || ""}, function (er, un) {
|
41 | if (er) {
|
42 | return cb(er.message === "cancelled" ? er.message : er)
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | if (!un) {
|
51 | return readUsername(c, u, cb)
|
52 | }
|
53 |
|
54 | var error = v(un)
|
55 | if (error) {
|
56 | log.warn(error.message)
|
57 | return readUsername(c, u, cb)
|
58 | }
|
59 |
|
60 | c.changed = c.u !== un
|
61 | u.u = un
|
62 | cb(er)
|
63 | })
|
64 | }
|
65 |
|
66 | function readPassword (c, u, cb) {
|
67 | var v = userValidate.pw
|
68 |
|
69 | if (!c.changed) {
|
70 | u.p = c.p
|
71 | return cb()
|
72 | }
|
73 | read({prompt: "Password: ", silent: true}, function (er, pw) {
|
74 | if (er) {
|
75 | return cb(er.message === "cancelled" ? er.message : er)
|
76 | }
|
77 |
|
78 | if (!pw) {
|
79 | return readPassword(c, u, cb)
|
80 | }
|
81 |
|
82 | var error = v(pw)
|
83 | if (error) {
|
84 | log.warn(error.message)
|
85 | return readPassword(c, u, cb)
|
86 | }
|
87 |
|
88 | u.p = pw
|
89 | cb(er)
|
90 | })
|
91 | }
|
92 |
|
93 | function readEmail (c, u, cb) {
|
94 | var v = userValidate.email
|
95 | var r = { prompt: "Email: ", default: c.e || "" }
|
96 | read(r, function (er, em) {
|
97 | if (er) {
|
98 | return cb(er.message === "cancelled" ? er.message : er)
|
99 | }
|
100 |
|
101 | if (!em) {
|
102 | return readEmail(c, u, cb)
|
103 | }
|
104 |
|
105 | var error = v(em)
|
106 | if (error) {
|
107 | log.warn(error.message)
|
108 | return readEmail(c, u, cb)
|
109 | }
|
110 |
|
111 | u.e = em
|
112 | cb(er)
|
113 | })
|
114 | }
|
115 |
|
116 | function save (c, u, cb) {
|
117 | if (c.changed) {
|
118 | delete registry.auth
|
119 | delete registry.username
|
120 | delete registry.password
|
121 | registry.username = u.u
|
122 | registry.password = u.p
|
123 | }
|
124 |
|
125 |
|
126 | registry.adduser(u.u, u.p, u.e, function (er) {
|
127 | if (er) return cb(er)
|
128 | registry.username = u.u
|
129 | registry.password = u.p
|
130 | registry.email = u.e
|
131 | npm.config.set("username", u.u, "user")
|
132 | npm.config.set("_password", u.p, "user")
|
133 | npm.config.set("email", u.e, "user")
|
134 | npm.config.del("_token", "user")
|
135 | log.info("adduser", "Authorized user %s", u.u)
|
136 | npm.config.save("user", cb)
|
137 | })
|
138 | }
|