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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
// defaults, types, and shorthands.
var path = require('path')
var url = require('url')
var Stream = require('stream').Stream
var semver = require('semver')
var stableFamily = semver.parse(process.version)
var nopt = require('nopt')
var os = require('os')
var osenv = require('osenv')
var umask = require('../utils/umask')
var hasUnicode = require('has-unicode')
var log
try {
log = require('npmlog')
} catch (er) {
var util = require('util')
log = { warn: function (m) {
console.warn(m + ' ' + util.format.apply(util, [].slice.call(arguments, 1)))
} }
}
exports.Umask = Umask
function Umask () {}
function validateUmask (data, k, val) {
return umask.validate(data, k, val)
}
function validateSemver (data, k, val) {
if (!semver.valid(val)) return false
data[k] = semver.valid(val)
}
function validateStream (data, k, val) {
if (!(val instanceof Stream)) return false
data[k] = val
}
nopt.typeDefs.semver = { type: semver, validate: validateSemver }
nopt.typeDefs.Stream = { type: Stream, validate: validateStream }
nopt.typeDefs.Umask = { type: Umask, validate: validateUmask }
nopt.invalidHandler = function (k, val, type) {
log.warn('invalid config', k + '=' + JSON.stringify(val))
if (Array.isArray(type)) {
if (type.indexOf(url) !== -1) type = url
else if (type.indexOf(path) !== -1) type = path
}
switch (type) {
case Umask:
log.warn('invalid config', 'Must be umask, octal number in range 0000..0777')
break
case url:
log.warn('invalid config', "Must be a full url with 'http://'")
break
case path:
log.warn('invalid config', 'Must be a valid filesystem path')
break
case Number:
log.warn('invalid config', 'Must be a numeric value')
break
case Stream:
log.warn('invalid config', 'Must be an instance of the Stream class')
break
}
}
if (!stableFamily || (+stableFamily.minor % 2)) stableFamily = null
else stableFamily = stableFamily.major + '.' + stableFamily.minor
var defaults
var temp = osenv.tmpdir()
var home = osenv.home()
var uidOrPid = process.getuid ? process.getuid() : process.pid
if (home) process.env.HOME = home
else home = path.resolve(temp, 'npm-' + uidOrPid)
var cacheExtra = process.platform === 'win32' ? 'npm-cache' : '.npm'
var cacheRoot = process.platform === 'win32' && process.env.APPDATA || home
var cache = path.resolve(cacheRoot, cacheExtra)
var globalPrefix
Object.defineProperty(exports, 'defaults', {get: function () {
if (defaults) return defaults
if (process.env.PREFIX) {
globalPrefix = process.env.PREFIX
} else if (process.platform === 'win32') {
// c:\node\node.exe --> prefix=c:\node\
globalPrefix = path.dirname(process.execPath)
} else {
// /usr/local/bin/node --> prefix=/usr/local
globalPrefix = path.dirname(path.dirname(process.execPath))
// destdir only is respected on Unix
if (process.env.DESTDIR) {
globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
}
}
defaults = {
access: null,
'allow-same-version': false,
'always-auth': false,
also: null,
'auth-type': 'legacy',
'bin-links': true,
browser: null,
ca: null,
cafile: null,
cache: cache,
'cache-lock-stale': 60000,
'cache-lock-retries': 10,
'cache-lock-wait': 10000,
'cache-max': Infinity,
'cache-min': 10,
cert: null,
color: true,
depth: Infinity,
description: true,
dev: false,
'dry-run': false,
editor: osenv.editor(),
'engine-strict': false,
force: false,
'fetch-retries': 2,
'fetch-retry-factor': 10,
'fetch-retry-mintimeout': 10000,
'fetch-retry-maxtimeout': 60000,
git: 'git',
'git-tag-version': true,
'commit-hooks': true,
global: false,
globalconfig: path.resolve(globalPrefix, 'etc', 'npmrc'),
'global-style': false,
group: process.platform === 'win32' ? 0
: process.env.SUDO_GID || (process.getgid && process.getgid()),
'ham-it-up': false,
heading: 'npm',
'if-present': false,
'ignore-prepublish': false,
'ignore-scripts': false,
'init-module': path.resolve(home, '.npm-init.js'),
'init-author-name': '',
'init-author-email': '',
'init-author-url': '',
'init-version': '1.0.0',
'init-license': 'ISC',
json: false,
key: null,
'legacy-bundling': false,
link: false,
'local-address': undefined,
loglevel: 'notice',
logstream: process.stderr,
'logs-max': 10,
long: false,
maxsockets: 50,
message: '%s',
'metrics-registry': null,
'node-version': process.version,
'offline': false,
'onload-script': false,
only: null,
optional: true,
'package-lock': true,
parseable: false,
'prefer-offline': false,
'prefer-online': false,
prefix: globalPrefix,
production: process.env.NODE_ENV === 'production',
'progress': !process.env.TRAVIS && !process.env.CI,
'proprietary-attribs': true,
proxy: null,
'https-proxy': null,
'user-agent': 'npm/{npm-version} ' +
'node/{node-version} ' +
'{platform} ' +
'{arch}',
'rebuild-bundle': true,
registry: 'https://registry.npmjs.org/',
rollback: true,
save: true,
'save-bundle': false,
'save-dev': false,
'save-exact': false,
'save-optional': false,
'save-prefix': '^',
'save-prod': false,
scope: '',
'script-shell': null,
'scripts-prepend-node-path': 'warn-only',
searchopts: '',
searchexclude: null,
searchlimit: 20,
searchstaleness: 15 * 60,
'send-metrics': false,
shell: osenv.shell(),
shrinkwrap: true,
'sign-git-tag': false,
'sso-poll-frequency': 500,
'sso-type': 'oauth',
'strict-ssl': true,
tag: 'latest',
'tag-version-prefix': 'v',
timing: false,
tmp: temp,
unicode: hasUnicode(),
'unsafe-perm': process.platform === 'win32' ||
process.platform === 'cygwin' ||
!(process.getuid && process.setuid &&
process.getgid && process.setgid) ||
process.getuid() !== 0,
usage: false,
user: process.platform === 'win32' ? 0 : 'nobody',
userconfig: path.resolve(home, '.npmrc'),
umask: process.umask ? process.umask() : umask.fromString('022'),
version: false,
versions: false,
viewer: process.platform === 'win32' ? 'browser' : 'man',
_exit: true
}
return defaults
}})
exports.types = {
access: [null, 'restricted', 'public'],
'allow-same-version': Boolean,
'always-auth': Boolean,
also: [null, 'dev', 'development'],
'auth-type': ['legacy', 'sso', 'saml', 'oauth'],
'bin-links': Boolean,
browser: [null, String],
ca: [null, String, Array],
cafile: path,
cache: path,
'cache-lock-stale': Number,
'cache-lock-retries': Number,
'cache-lock-wait': Number,
'cache-max': Number,
'cache-min': Number,
cert: [null, String],
color: ['always', Boolean],
depth: Number,
description: Boolean,
dev: Boolean,
'dry-run': Boolean,
editor: String,
'engine-strict': Boolean,
force: Boolean,
'fetch-retries': Number,
'fetch-retry-factor': Number,
'fetch-retry-mintimeout': Number,
'fetch-retry-maxtimeout': Number,
git: String,
'git-tag-version': Boolean,
'commit-hooks': Boolean,
global: Boolean,
globalconfig: path,
'global-style': Boolean,
group: [Number, String],
'https-proxy': [null, url],
'user-agent': String,
'ham-it-up': Boolean,
'heading': String,
'if-present': Boolean,
'ignore-prepublish': Boolean,
'ignore-scripts': Boolean,
'init-module': path,
'init-author-name': String,
'init-author-email': String,
'init-author-url': ['', url],
'init-license': String,
'init-version': semver,
json: Boolean,
key: [null, String],
'legacy-bundling': Boolean,
link: Boolean,
// local-address must be listed as an IP for a local network interface
// must be IPv4 due to node bug
'local-address': getLocalAddresses(),
loglevel: ['silent', 'error', 'warn', 'notice', 'http', 'timing', 'info', 'verbose', 'silly'],
logstream: Stream,
'logs-max': Number,
long: Boolean,
maxsockets: Number,
message: String,
'metrics-registry': [null, String],
'node-version': [null, semver],
offline: Boolean,
'onload-script': [null, String],
only: [null, 'dev', 'development', 'prod', 'production'],
optional: Boolean,
'package-lock': Boolean,
parseable: Boolean,
'prefer-offline': Boolean,
'prefer-online': Boolean,
prefix: path,
production: Boolean,
progress: Boolean,
'proprietary-attribs': Boolean,
proxy: [null, false, url], // allow proxy to be disabled explicitly
'rebuild-bundle': Boolean,
registry: [null, url],
rollback: Boolean,
save: Boolean,
'save-bundle': Boolean,
'save-dev': Boolean,
'save-exact': Boolean,
'save-optional': Boolean,
'save-prefix': String,
'save-prod': Boolean,
scope: String,
'script-shell': [null, String],
'scripts-prepend-node-path': [false, true, 'auto', 'warn-only'],
searchopts: String,
searchexclude: [null, String],
searchlimit: Number,
searchstaleness: Number,
'send-metrics': Boolean,
shell: String,
shrinkwrap: Boolean,
'sign-git-tag': Boolean,
'sso-poll-frequency': Number,
'sso-type': [null, 'oauth', 'saml'],
'strict-ssl': Boolean,
tag: String,
timing: Boolean,
tmp: path,
unicode: Boolean,
'unsafe-perm': Boolean,
usage: Boolean,
user: [Number, String],
userconfig: path,
umask: Umask,
version: Boolean,
'tag-version-prefix': String,
versions: Boolean,
viewer: String,
_exit: Boolean
}
function getLocalAddresses () {
var interfaces
// #8094: some environments require elevated permissions to enumerate
// interfaces, and synchronously throw EPERM when run without
// elevated privileges
try {
interfaces = os.networkInterfaces()
} catch (e) {
interfaces = {}
}
return Object.keys(interfaces).map(function (nic) {
return interfaces[nic].filter(function (addr) {
return addr.family === 'IPv4'
})
.map(function (addr) {
return addr.address
})
}).reduce(function (curr, next) {
return curr.concat(next)
}, []).concat(undefined)
}
exports.shorthands = {
s: ['--loglevel', 'silent'],
d: ['--loglevel', 'info'],
dd: ['--loglevel', 'verbose'],
ddd: ['--loglevel', 'silly'],
noreg: ['--no-registry'],
N: ['--no-registry'],
reg: ['--registry'],
'no-reg': ['--no-registry'],
silent: ['--loglevel', 'silent'],
verbose: ['--loglevel', 'verbose'],
quiet: ['--loglevel', 'warn'],
q: ['--loglevel', 'warn'],
h: ['--usage'],
H: ['--usage'],
'?': ['--usage'],
help: ['--usage'],
v: ['--version'],
f: ['--force'],
desc: ['--description'],
'no-desc': ['--no-description'],
'local': ['--no-global'],
l: ['--long'],
m: ['--message'],
p: ['--parseable'],
porcelain: ['--parseable'],
g: ['--global'],
S: ['--save'],
D: ['--save-dev'],
E: ['--save-exact'],
O: ['--save-optional'],
P: ['--save-prod'],
y: ['--yes'],
n: ['--no-yes'],
B: ['--save-bundle'],
C: ['--prefix']
}
|