1 |
|
2 |
|
3 |
|
4 | var path = require("path")
|
5 | , url = require("url")
|
6 | , Stream = require("stream").Stream
|
7 | , semver = require("semver")
|
8 | , stableFamily = semver.parse(process.version)
|
9 | , nopt = require("nopt")
|
10 | , os = require("os")
|
11 | , osenv = require("osenv")
|
12 |
|
13 | var log
|
14 | try {
|
15 | log = require("npmlog")
|
16 | } catch (er) {
|
17 | var util = require("util")
|
18 | log = { warn: function (m) {
|
19 | console.warn(m + util.format.apply(util, [].slice.call(arguments, 1)))
|
20 | } }
|
21 | }
|
22 |
|
23 | exports.Octal = Octal
|
24 | function Octal () {}
|
25 | function validateOctal (data, k, val) {
|
26 |
|
27 | if (typeof val === "number") {
|
28 | data[k] = val
|
29 | return true
|
30 | }
|
31 |
|
32 | if (typeof val === "string") {
|
33 | if (val.charAt(0) !== "0" || isNaN(val)) return false
|
34 | data[k] = parseInt(val, 8).toString(8)
|
35 | }
|
36 | }
|
37 |
|
38 | function validateSemver (data, k, val) {
|
39 | if (!semver.valid(val)) return false
|
40 | data[k] = semver.valid(val)
|
41 | }
|
42 |
|
43 | function validateStream (data, k, val) {
|
44 | if (!(val instanceof Stream)) return false
|
45 | data[k] = val
|
46 | }
|
47 |
|
48 | nopt.typeDefs.semver = { type: semver, validate: validateSemver }
|
49 | nopt.typeDefs.Octal = { type: Octal, validate: validateOctal }
|
50 | nopt.typeDefs.Stream = { type: Stream, validate: validateStream }
|
51 |
|
52 | nopt.invalidHandler = function (k, val, type) {
|
53 | log.warn("invalid config", k + "=" + JSON.stringify(val))
|
54 |
|
55 | if (Array.isArray(type)) {
|
56 | if (type.indexOf(url) !== -1) type = url
|
57 | else if (type.indexOf(path) !== -1) type = path
|
58 | }
|
59 |
|
60 | switch (type) {
|
61 | case Octal:
|
62 | log.warn("invalid config", "Must be octal number, starting with 0")
|
63 | break
|
64 | case url:
|
65 | log.warn("invalid config", "Must be a full url with 'http://'")
|
66 | break
|
67 | case path:
|
68 | log.warn("invalid config", "Must be a valid filesystem path")
|
69 | break
|
70 | case Number:
|
71 | log.warn("invalid config", "Must be a numeric value")
|
72 | break
|
73 | case Stream:
|
74 | log.warn("invalid config", "Must be an instance of the Stream class")
|
75 | break
|
76 | }
|
77 | }
|
78 |
|
79 | if (!stableFamily || (+stableFamily.minor % 2)) stableFamily = null
|
80 | else stableFamily = stableFamily.major + "." + stableFamily.minor
|
81 |
|
82 | var defaults
|
83 |
|
84 | var temp = osenv.tmpdir()
|
85 | var home = osenv.home()
|
86 |
|
87 | var uidOrPid = process.getuid ? process.getuid() : process.pid
|
88 |
|
89 | if (home) process.env.HOME = home
|
90 | else home = path.resolve(temp, "npm-" + uidOrPid)
|
91 |
|
92 | var cacheExtra = process.platform === "win32" ? "npm-cache" : ".npm"
|
93 | var cacheRoot = process.platform === "win32" && process.env.APPDATA || home
|
94 | var cache = path.resolve(cacheRoot, cacheExtra)
|
95 |
|
96 |
|
97 | var globalPrefix
|
98 | Object.defineProperty(exports, "defaults", {get: function () {
|
99 | if (defaults) return defaults
|
100 |
|
101 | if (process.env.PREFIX) {
|
102 | globalPrefix = process.env.PREFIX
|
103 | } else if (process.platform === "win32") {
|
104 |
|
105 | globalPrefix = path.dirname(process.execPath)
|
106 | } else {
|
107 |
|
108 | globalPrefix = path.dirname(path.dirname(process.execPath))
|
109 |
|
110 |
|
111 | if (process.env.DESTDIR) {
|
112 | globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
|
113 | }
|
114 | }
|
115 |
|
116 | return defaults =
|
117 | { "always-auth" : false
|
118 | , "bin-links" : true
|
119 | , browser : null
|
120 |
|
121 | , ca: null
|
122 | , cafile: null
|
123 |
|
124 | , cache : cache
|
125 |
|
126 | , "cache-lock-stale": 60000
|
127 | , "cache-lock-retries": 10
|
128 | , "cache-lock-wait": 10000
|
129 |
|
130 | , "cache-max": Infinity
|
131 | , "cache-min": 10
|
132 |
|
133 | , cert: null
|
134 |
|
135 | , color : true
|
136 | , depth: Infinity
|
137 | , description : true
|
138 | , dev : false
|
139 | , editor : osenv.editor()
|
140 | , email: ""
|
141 | , "engine-strict": false
|
142 | , force : false
|
143 |
|
144 | , "fetch-retries": 2
|
145 | , "fetch-retry-factor": 10
|
146 | , "fetch-retry-mintimeout": 10000
|
147 | , "fetch-retry-maxtimeout": 60000
|
148 |
|
149 | , git: "git"
|
150 | , "git-tag-version": true
|
151 |
|
152 | , global : false
|
153 | , globalconfig : path.resolve(globalPrefix, "etc", "npmrc")
|
154 | , group : process.platform === "win32" ? 0
|
155 | : process.env.SUDO_GID || (process.getgid && process.getgid())
|
156 | , heading: "npm"
|
157 | , "ignore-scripts": false
|
158 | , "init-module": path.resolve(home, ".npm-init.js")
|
159 | , "init.author.name" : ""
|
160 | , "init.author.email" : ""
|
161 | , "init.author.url" : ""
|
162 | , "init.license": "ISC"
|
163 | , json: false
|
164 | , key: null
|
165 | , link: false
|
166 | , "local-address" : undefined
|
167 | , loglevel : "warn"
|
168 | , logstream : process.stderr
|
169 | , long : false
|
170 | , message : "%s"
|
171 | , "node-version" : process.version
|
172 | , npat : false
|
173 | , "onload-script" : false
|
174 | , optional: true
|
175 | , parseable : false
|
176 | , prefix : globalPrefix
|
177 | , production: process.env.NODE_ENV === "production"
|
178 | , "proprietary-attribs": true
|
179 | , proxy : process.env.HTTP_PROXY || process.env.http_proxy || null
|
180 | , "https-proxy" : process.env.HTTPS_PROXY || process.env.https_proxy ||
|
181 | process.env.HTTP_PROXY || process.env.http_proxy || null
|
182 | , "user-agent" : "npm/{npm-version} "
|
183 | + "node/{node-version} "
|
184 | + "{platform} "
|
185 | + "{arch}"
|
186 | , "rebuild-bundle" : true
|
187 | , registry : "https://registry.npmjs.org/"
|
188 | , rollback : true
|
189 | , save : false
|
190 | , "save-bundle": false
|
191 | , "save-dev" : false
|
192 | , "save-exact" : false
|
193 | , "save-optional" : false
|
194 | , "save-prefix": "^"
|
195 | , scope : ""
|
196 | , searchopts: ""
|
197 | , searchexclude: null
|
198 | , searchsort: "name"
|
199 | , shell : osenv.shell()
|
200 | , shrinkwrap: true
|
201 | , "sign-git-tag": false
|
202 | , spin: true
|
203 | , "strict-ssl": true
|
204 | , tag : "latest"
|
205 | , tmp : temp
|
206 | , unicode : true
|
207 | , "unsafe-perm" : process.platform === "win32"
|
208 | || process.platform === "cygwin"
|
209 | || !( process.getuid && process.setuid
|
210 | && process.getgid && process.setgid )
|
211 | || process.getuid() !== 0
|
212 | , usage : false
|
213 | , user : process.platform === "win32" ? 0 : "nobody"
|
214 | , username : ""
|
215 | , userconfig : path.resolve(home, ".npmrc")
|
216 | , umask: process.umask ? process.umask() : parseInt("022", 8)
|
217 | , version : false
|
218 | , versions : false
|
219 | , viewer: process.platform === "win32" ? "browser" : "man"
|
220 |
|
221 | , _exit : true
|
222 | }
|
223 | }})
|
224 |
|
225 | exports.types =
|
226 | { "always-auth" : Boolean
|
227 | , "bin-links": Boolean
|
228 | , browser : [null, String]
|
229 | , ca: [null, String, Array]
|
230 | , cafile : path
|
231 | , cache : path
|
232 | , "cache-lock-stale": Number
|
233 | , "cache-lock-retries": Number
|
234 | , "cache-lock-wait": Number
|
235 | , "cache-max": Number
|
236 | , "cache-min": Number
|
237 | , cert: [null, String]
|
238 | , color : ["always", Boolean]
|
239 | , depth : Number
|
240 | , description : Boolean
|
241 | , dev : Boolean
|
242 | , editor : String
|
243 | , email: [null, String]
|
244 | , "engine-strict": Boolean
|
245 | , force : Boolean
|
246 | , "fetch-retries": Number
|
247 | , "fetch-retry-factor": Number
|
248 | , "fetch-retry-mintimeout": Number
|
249 | , "fetch-retry-maxtimeout": Number
|
250 | , git: String
|
251 | , "git-tag-version": Boolean
|
252 | , global : Boolean
|
253 | , globalconfig : path
|
254 | , group : [Number, String]
|
255 | , "https-proxy" : [null, url]
|
256 | , "user-agent" : String
|
257 | , "heading": String
|
258 | , "ignore-scripts": Boolean
|
259 | , "init-module": path
|
260 | , "init.author.name" : String
|
261 | , "init.author.email" : String
|
262 | , "init.author.url" : ["", url]
|
263 | , "init.license": String
|
264 | , json: Boolean
|
265 | , key: [null, String]
|
266 | , link: Boolean
|
267 |
|
268 |
|
269 | , "local-address" : getLocalAddresses()
|
270 | , loglevel : ["silent","win","error","warn","http","info","verbose","silly"]
|
271 | , logstream : Stream
|
272 | , long : Boolean
|
273 | , message: String
|
274 | , "node-version" : [null, semver]
|
275 | , npat : Boolean
|
276 | , "onload-script" : [null, String]
|
277 | , optional: Boolean
|
278 | , parseable : Boolean
|
279 | , prefix: path
|
280 | , production: Boolean
|
281 | , "proprietary-attribs": Boolean
|
282 | , proxy : [null, url]
|
283 | , "rebuild-bundle" : Boolean
|
284 | , registry : [null, url]
|
285 | , rollback : Boolean
|
286 | , save : Boolean
|
287 | , "save-bundle": Boolean
|
288 | , "save-dev" : Boolean
|
289 | , "save-exact" : Boolean
|
290 | , "save-optional" : Boolean
|
291 | , "save-prefix": String
|
292 | , scope : String
|
293 | , searchopts : String
|
294 | , searchexclude: [null, String]
|
295 | , searchsort: [ "name", "-name"
|
296 | , "description", "-description"
|
297 | , "author", "-author"
|
298 | , "date", "-date"
|
299 | , "keywords", "-keywords" ]
|
300 | , shell : String
|
301 | , shrinkwrap: Boolean
|
302 | , "sign-git-tag": Boolean
|
303 | , spin: ["always", Boolean]
|
304 | , "strict-ssl": Boolean
|
305 | , tag : String
|
306 | , tmp : path
|
307 | , unicode : Boolean
|
308 | , "unsafe-perm" : Boolean
|
309 | , usage : Boolean
|
310 | , user : [Number, String]
|
311 | , username : String
|
312 | , userconfig : path
|
313 | , umask: Octal
|
314 | , version : Boolean
|
315 | , versions : Boolean
|
316 | , viewer: String
|
317 | , _exit : Boolean
|
318 | , _password: String
|
319 | }
|
320 |
|
321 | function getLocalAddresses() {
|
322 | Object.keys(os.networkInterfaces()).map(function (nic) {
|
323 | return os.networkInterfaces()[nic].filter(function (addr) {
|
324 | return addr.family === "IPv4"
|
325 | })
|
326 | .map(function (addr) {
|
327 | return addr.address
|
328 | })
|
329 | }).reduce(function (curr, next) {
|
330 | return curr.concat(next)
|
331 | }, []).concat(undefined)
|
332 | }
|
333 |
|
334 | exports.shorthands =
|
335 | { s : ["--loglevel", "silent"]
|
336 | , d : ["--loglevel", "info"]
|
337 | , dd : ["--loglevel", "verbose"]
|
338 | , ddd : ["--loglevel", "silly"]
|
339 | , noreg : ["--no-registry"]
|
340 | , N : ["--no-registry"]
|
341 | , reg : ["--registry"]
|
342 | , "no-reg" : ["--no-registry"]
|
343 | , silent : ["--loglevel", "silent"]
|
344 | , verbose : ["--loglevel", "verbose"]
|
345 | , quiet: ["--loglevel", "warn"]
|
346 | , q: ["--loglevel", "warn"]
|
347 | , h : ["--usage"]
|
348 | , H : ["--usage"]
|
349 | , "?" : ["--usage"]
|
350 | , help : ["--usage"]
|
351 | , v : ["--version"]
|
352 | , f : ["--force"]
|
353 | , gangster : ["--force"]
|
354 | , gangsta : ["--force"]
|
355 | , desc : ["--description"]
|
356 | , "no-desc" : ["--no-description"]
|
357 | , "local" : ["--no-global"]
|
358 | , l : ["--long"]
|
359 | , m : ["--message"]
|
360 | , p : ["--parseable"]
|
361 | , porcelain : ["--parseable"]
|
362 | , g : ["--global"]
|
363 | , S : ["--save"]
|
364 | , D : ["--save-dev"]
|
365 | , E : ["--save-exact"]
|
366 | , O : ["--save-optional"]
|
367 | , y : ["--yes"]
|
368 | , n : ["--no-yes"]
|
369 | , B : ["--save-bundle"]
|
370 | }
|