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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
module.exports = config
config.usage = "npm config set <key> <value>"
+ "\nnpm config get [<key>]"
+ "\nnpm config delete <key>"
+ "\nnpm config list"
+ "\nnpm config edit"
+ "\nnpm set <key> <value>"
+ "\nnpm get [<key>]"
var ini = require("./utils/ini.js")
, log = require("npmlog")
, npm = require("./npm.js")
, exec = require("./utils/exec.js")
, fs = require("graceful-fs")
, dc
, types = require("./utils/config-defs.js").types
config.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
if (argv[1] !== "config") argv.unshift("config")
if (argv.length === 2) {
var cmds = ["get", "set", "delete", "ls", "rm", "edit"]
if (opts.partialWord !== "l") cmds.push("list")
return cb(null, cmds)
}
var action = argv[2]
switch (action) {
case "set":
// todo: complete with valid values, if possible.
if (argv.length > 3) return cb(null, [])
// fallthrough
case "get":
case "delete":
case "rm":
return cb(null, Object.keys(types))
case "edit":
case "list": case "ls":
return cb(null, [])
default: return cb(null, [])
}
}
// npm config set key value
// npm config get key
// npm config list
function config (args, cb) {
var action = args.shift()
switch (action) {
case "set": return set(args[0], args[1], cb)
case "get": return get(args[0], cb)
case "delete": case "rm": case "del": return del(args[0], cb)
case "list": case "ls": return list(cb)
case "edit": return edit(cb)
default: return unknown(action, cb)
}
}
function edit (cb) {
var e = ini.get("editor")
, which = ini.get("global") ? "global" : "user"
, f = ini.get(which + "config")
, eol = process.platform === "win32" ? "\r\n" : "\n"
if (!e) return cb(new Error("No EDITOR config or environ set."))
ini.save(which, function (er) {
if (er) return cb(er)
fs.readFile(f, "utf8", function (er, data) {
if (er) data = ""
dc = dc || require("./utils/config-defs.js").defaults
data = [ ";;;;"
, "; npm "+(ini.get("global") ?
"globalconfig" : "userconfig")+" file"
, "; this is a simple ini-formatted file"
, "; lines that start with semi-colons are comments."
, "; read `npm help config` for help on the various options"
, ";;;;"
, ""
, data
].concat( [ ";;;;"
, "; all options with default values"
, ";;;;"
]
)
.concat(Object.keys(dc).map(function (k) {
return "; " + k + " = " + ini.unParseField(dc[k],k)
}))
.concat([""])
.join(eol)
fs.writeFile
( f
, data
, "utf8"
, function (er) {
if (er) return cb(er)
exec(e, [f], function (er) {
if (er) return cb(er)
ini.resolveConfigs(function (er) {
if (er) return cb(er)
ini.save(which, cb)
})
})
}
)
})
})
}
function del (key, cb) {
if (!key) return cb(new Error("no key provided"))
ini.del(key)
ini.save(ini.get("global") ? "global" : "user", cb)
}
function set (key, val, cb) {
if (key === undefined) {
return unknown("", cb)
}
if (val === undefined) {
if (key.indexOf("=") !== -1) {
var k = key.split("=")
key = k.shift()
val = k.join("=")
} else {
val = ""
}
}
key = key.trim()
val = val.trim()
log.info("config", "set %j %j", key, val)
var where = ini.get("global") ? "global" : "user"
ini.set(key, val, where)
ini.save(where, cb)
}
function get (key, cb) {
if (!key) return list(cb)
if (key.charAt(0) === "_") {
return cb(new Error("---sekretz---"))
}
console.log(npm.config.get(key))
cb()
}
function sort (a, b) {
return a > b ? 1 : -1
}
function reverse (a, b) {
return a > b ? -1 : 1
}
function list (cb) {
var msg = ""
, long = npm.config.get("long")
// cli configs.
// show any that aren't secret
var cli = ini.configList.list[ini.TRANS.cli]
, eol = process.platform === "win32" ? "\r\n" : "\n"
, cliKeys = Object.keys(cli).filter(function (k) {
return !(k.charAt(0) === "_" || types[k] !== types[k])
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (cliKeys.length) {
msg += "; cli configs" + eol
cliKeys.forEach(function (k) {
if (cli[k] && typeof cli[k] === "object") return
if (k === "argv") return
msg += k + " = " + JSON.stringify(cli[k]) + eol
})
msg += eol
}
// env configs
var env = ini.configList.list[ini.TRANS.env]
, envKeys = Object.keys(env).filter(function (k) {
return !(k.charAt(0) === "_" || types[k] !== types[k])
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (envKeys.length) {
msg += "; environment configs" + eol
envKeys.forEach(function (k) {
if (env[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + JSON.stringify(env[k])
+ " (overridden)" + eol
} else msg += k + " = " + JSON.stringify(env[k]) + eol
})
msg += eol
}
// user config file
var uconf = ini.configList.list[ini.TRANS.user]
, uconfKeys = Object.keys(uconf).filter(function (k) {
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (uconfKeys.length) {
msg += "; userconfig " + ini.get("userconfig") + eol
uconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(uconf[k])
if (uconf[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
})
msg += eol
}
// global config file
var gconf = ini.configList.list[ini.TRANS.global]
, gconfKeys = Object.keys(gconf).filter(function (k) {
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (gconfKeys.length) {
msg += "; globalconfig " + ini.get("globalconfig") + eol
gconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(gconf[k])
if (gconf[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
})
msg += eol
}
// builtin config file
var bconf = ini.configList.list[ini.TRANS.builtin]
, bconfKeys = Object.keys(bconf).filter(function (k) {
return types[k] === types[k]
}).sort(function (a, b) {
return a > b ? 1 : -1
})
if (bconfKeys.length) {
var path = require("path")
msg += "; builtin config " + path.resolve(__dirname, "../npmrc") + eol
bconfKeys.forEach(function (k) {
var val = (k.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(bconf[k])
if (bconf[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
})
msg += eol
}
// only show defaults if --long
if (!long) {
msg += "; node install prefix = " + process.installPrefix + eol
+ "; node bin location = " + process.execPath + eol
+ "; cwd = " + process.cwd() + eol
+ "; HOME = " + process.env.HOME + eol
+ "; 'npm config ls -l' to show all defaults." + eol
console.log(msg)
return cb()
}
var defaults = ini.defaultConfig
, defKeys = Object.keys(defaults)
msg += "; default values" + eol
defKeys.forEach(function (k) {
if (defaults[k] && typeof defaults[k] === "object") return
var val = JSON.stringify(defaults[k])
if (defaults[k] !== ini.get(k)) {
if (!long) return
msg += "; " + k + " = " + val
+ " (overridden)" + eol
} else msg += k + " = " + val + eol
})
msg += eol
console.log(msg)
return cb()
}
function unknown (action, cb) {
cb("Usage:\n" + config.usage)
}
|