This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
config.js
141 lines (129 loc) · 3.98 KB
/
config.js
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
module.exports = config
config.usage = "npm config set <key> <value>"
+ "\nnpm config get <key>"
+ "\nnpm config delete <key>"
+ "\nnpm config list"
+ "\nnpm config edit"
config.completion = function(args, index, cb) {
var getCompletions = require("./utils/completion/get-completions")
, subcmdList = ["set", "get", "ls", "delete", "edit"]
, takeKeys = ["set", "get", "delete"]
, subcmd = args[0] || ""
, key = args[1] || ""
if (subcmdList.indexOf(subcmd) !== -1) {
if (takeKeys.indexOf(subcmd) !== -1 && ini.keys.indexOf(key) === -1) {
cb(null, getCompletions(key, ini.keys))
}
} else cb(null, getCompletions(subcmd, subcmdList))
}
var ini = require("./utils/ini")
, log = require("./utils/log")
, npm = require("../npm")
, exec = require("./utils/exec")
, fs = require("./utils/graceful-fs")
, dc
, output = require("./utils/output")
, parseArgs = require("./utils/parse-args")
// 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")
, f = ini.get(ini.get("global") ? "globalconfig" : "userconfig")
if (!e) return cb(new Error("No EDITOR config or environ set."))
ini.save(function (er) {
if (er) return cb(er)
fs.readFile(f, "utf8", function (er, data) {
if (er) data = ""
dc = dc || require("./utils/default-config")
data = [ ";;;;"
, "; npm "+(ini.get("global") ? "globalconfig" : "userconfig")+" file"
, "; this is a simple ini-formatted file"
, "; lines that start with semi-colons are comments, and removed."
, "; 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("\n")
fs.writeFile
( f
, data
, "utf8"
, function (er) {
if (er) return cb(er)
exec("sh", ["-c", e + " "+f], function (er) {
if (er) return cb(er)
ini.resolveConfigs(function (er) {
if (er) return cb(er)
ini.save(cb)
})
})
}
)
})
})
}
function del (key, cb) {
if (!key) return cb(new Error("no key provided"))
ini.del(key)
ini.save(cb)
}
function set (key, val, cb) {
if (val === undefined && key.indexOf("=") !== -1) {
var k = key.split("=")
key = k.shift()
val = k.join("=")
}
key = key.trim()
val = val.trim()
log("set "+key+" "+val, "config")
var where = ini.get("global") ? "global" : "user"
ini.set(key, val, where)
ini.save(cb)
}
function get (key, cb) {
var outfd = npm.config.get("outfd")
if (!key) return list(cb)
if (key.charAt(0) === "_") {
return cb(new Error("---sekretz---"))
}
output.write(outfd, npm.config.get(key), cb)
}
function list (cb) {
var msg = ""
, outfd = npm.config.get("outfd")
ini.keys.sort(function (a,b) { return a > b ? 1 : -1 })
.forEach(function (i) {
if (parseArgs.types[i] !== parseArgs.types[i]) {
return
}
var val = (i.charAt(0) === "_")
? "---sekretz---"
: JSON.stringify(ini.get(i))
msg += i.replace(/^_/,';_')+" = "+val+"\n"
})
output.write(outfd, msg, cb)
}
function unknown (action, cb) {
cb("Usage:\n" + config.usage)
}