Skip to content

Commit 48753bb

Browse files
committed
a utility for config defaults, types, and shorthands
1 parent b983f47 commit 48753bb

File tree

3 files changed

+3883
-0
lines changed

3 files changed

+3883
-0
lines changed

lib/utils/config.js

Lines changed: 367 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,367 @@
1+
// defaults, types, and shorthands
2+
3+
const {
4+
typeDefs: {
5+
semver: { type: semver },
6+
Umask: { type: Umask },
7+
url: { type: url },
8+
path: { type: path }
9+
}
10+
} = require('@npmcli/config')
11+
12+
const { dirname } = require('path')
13+
const isWindows = require('./is-windows.js')
14+
15+
const editor = process.env.EDITOR ||
16+
process.env.VISUAL ||
17+
(isWindows ? 'notepad.exe' : 'vi')
18+
19+
const shell = isWindows ? process.env.ComSpec || 'cmd'
20+
: process.env.SHELL || 'bash'
21+
22+
const { tmpdir, networkInterfaces } = require('os')
23+
const getLocalAddresses = () => {
24+
try {
25+
return Object.values(networkInterfaces()).map(
26+
int => int.map(({ address }) => address)
27+
).reduce((set, addrs) => set.concat(addrs), [undefined])
28+
} catch (e) {
29+
return [undefined]
30+
}
31+
}
32+
33+
const unicode = /UTF-?8$/i.test(
34+
process.env.LC_ALL || process.env.LC_CTYPE || process.env.LANG
35+
)
36+
37+
// use LOCALAPPDATA on Windows, if set
38+
// https://github.com/npm/cli/pull/899
39+
const cacheRoot = isWindows && process.env.LOCALAPPDATA || '~'
40+
const cacheExtra = isWindows ? 'npm-cache' : '.npm'
41+
const cache = `${cacheRoot}/${cacheExtra}`
42+
43+
const defaults = {
44+
access: null,
45+
all: false,
46+
'allow-same-version': false,
47+
'always-auth': false,
48+
also: null,
49+
audit: true,
50+
'audit-level': null,
51+
'auth-type': 'legacy',
52+
53+
before: null,
54+
'bin-links': true,
55+
browser: null,
56+
57+
ca: null,
58+
cafile: null,
59+
60+
cache,
61+
62+
'cache-lock-stale': 60000,
63+
'cache-lock-retries': 10,
64+
'cache-lock-wait': 10000,
65+
66+
'cache-max': Infinity,
67+
'cache-min': 10,
68+
69+
cert: null,
70+
71+
cidr: null,
72+
73+
color: process.env.NO_COLOR == null,
74+
call: '',
75+
depth: 0,
76+
description: true,
77+
dev: false,
78+
'dry-run': false,
79+
editor,
80+
'engine-strict': false,
81+
force: false,
82+
'format-package-lock': true,
83+
84+
fund: true,
85+
86+
'fetch-retries': 2,
87+
'fetch-retry-factor': 10,
88+
'fetch-retry-mintimeout': 10000,
89+
'fetch-retry-maxtimeout': 60000,
90+
91+
git: 'git',
92+
'git-tag-version': true,
93+
'commit-hooks': true,
94+
95+
global: false,
96+
'global-style': false,
97+
heading: 'npm',
98+
'if-present': false,
99+
include: [],
100+
'include-staged': false,
101+
'ignore-prepublish': false,
102+
'ignore-scripts': false,
103+
'init-module': '~/.npm-init.js',
104+
'init-author-name': '',
105+
'init-author-email': '',
106+
'init-author-url': '',
107+
'init-version': '1.0.0',
108+
'init-license': 'ISC',
109+
json: false,
110+
key: null,
111+
'legacy-bundling': false,
112+
'legacy-peer-deps': false,
113+
link: false,
114+
'local-address': undefined,
115+
loglevel: 'notice',
116+
'logs-max': 10,
117+
long: false,
118+
maxsockets: 50,
119+
message: '%s',
120+
'metrics-registry': null,
121+
'node-options': null,
122+
'node-version': process.version,
123+
offline: false,
124+
omit: [],
125+
only: null,
126+
optional: true,
127+
otp: null,
128+
package: [],
129+
'package-lock': true,
130+
'package-lock-only': false,
131+
parseable: false,
132+
'prefer-offline': false,
133+
'prefer-online': false,
134+
preid: '',
135+
production: process.env.NODE_ENV === 'production',
136+
progress: !process.env.TRAVIS && !process.env.CI,
137+
proxy: null,
138+
'https-proxy': null,
139+
noproxy: null,
140+
'user-agent': 'npm/{npm-version} ' +
141+
'node/{node-version} ' +
142+
'{platform} ' +
143+
'{arch} ' +
144+
'{ci}',
145+
'read-only': false,
146+
'rebuild-bundle': true,
147+
registry: 'https://registry.npmjs.org/',
148+
rollback: true,
149+
save: true,
150+
'save-bundle': false,
151+
'save-dev': false,
152+
'save-exact': false,
153+
'save-optional': false,
154+
'save-prefix': '^',
155+
'save-prod': false,
156+
scope: '',
157+
'script-shell': null,
158+
'scripts-prepend-node-path': 'warn-only',
159+
searchopts: '',
160+
searchexclude: null,
161+
searchlimit: 20,
162+
searchstaleness: 15 * 60,
163+
'send-metrics': false,
164+
shell,
165+
shrinkwrap: true,
166+
'sign-git-commit': false,
167+
'sign-git-tag': false,
168+
'sso-poll-frequency': 500,
169+
'sso-type': 'oauth',
170+
'strict-ssl': true,
171+
tag: 'latest',
172+
'tag-version-prefix': 'v',
173+
timing: false,
174+
tmp: tmpdir(),
175+
unicode,
176+
'update-notifier': true,
177+
usage: false,
178+
userconfig: '~/.npmrc',
179+
umask: process.umask ? process.umask() : 0o22,
180+
version: false,
181+
versions: false,
182+
viewer: isWindows ? 'browser' : 'man'
183+
}
184+
185+
const types = {
186+
access: [null, 'restricted', 'public'],
187+
all: Boolean,
188+
'allow-same-version': Boolean,
189+
'always-auth': Boolean,
190+
also: [null, 'dev', 'development'],
191+
audit: Boolean,
192+
'audit-level': ['low', 'moderate', 'high', 'critical', 'none', null],
193+
'auth-type': ['legacy', 'sso', 'saml', 'oauth'],
194+
before: [null, Date],
195+
'bin-links': Boolean,
196+
browser: [null, Boolean, String],
197+
ca: [null, String, Array],
198+
cafile: path,
199+
cache: path,
200+
'cache-lock-stale': Number,
201+
'cache-lock-retries': Number,
202+
'cache-lock-wait': Number,
203+
'cache-max': Number,
204+
'cache-min': Number,
205+
cert: [null, String],
206+
cidr: [null, String, Array],
207+
color: ['always', Boolean],
208+
call: String,
209+
depth: Number,
210+
description: Boolean,
211+
dev: Boolean,
212+
'dry-run': Boolean,
213+
editor: String,
214+
'engine-strict': Boolean,
215+
force: Boolean,
216+
fund: Boolean,
217+
'format-package-lock': Boolean,
218+
'fetch-retries': Number,
219+
'fetch-retry-factor': Number,
220+
'fetch-retry-mintimeout': Number,
221+
'fetch-retry-maxtimeout': Number,
222+
git: String,
223+
'git-tag-version': Boolean,
224+
'commit-hooks': Boolean,
225+
global: Boolean,
226+
globalconfig: path,
227+
'global-style': Boolean,
228+
group: [Number, String],
229+
'https-proxy': [null, url],
230+
'user-agent': String,
231+
'ham-it-up': Boolean,
232+
heading: String,
233+
'if-present': Boolean,
234+
include: [Array, 'prod', 'dev', 'optional', 'peer'],
235+
'include-staged': Boolean,
236+
'ignore-prepublish': Boolean,
237+
'ignore-scripts': Boolean,
238+
'init-module': path,
239+
'init-author-name': String,
240+
'init-author-email': String,
241+
'init-author-url': ['', url],
242+
'init-license': String,
243+
'init-version': semver,
244+
json: Boolean,
245+
key: [null, String],
246+
'legacy-bundling': Boolean,
247+
'legacy-peer-deps': Boolean,
248+
link: Boolean,
249+
'local-address': getLocalAddresses(),
250+
loglevel: [
251+
'silent',
252+
'error',
253+
'warn',
254+
'notice',
255+
'http',
256+
'timing',
257+
'info',
258+
'verbose',
259+
'silly'
260+
],
261+
'logs-max': Number,
262+
long: Boolean,
263+
maxsockets: Number,
264+
message: String,
265+
'metrics-registry': [null, String],
266+
'node-options': [null, String],
267+
'node-version': [null, semver],
268+
noproxy: [null, String, Array],
269+
offline: Boolean,
270+
omit: [Array, 'dev', 'optional', 'peer'],
271+
only: [null, 'dev', 'development', 'prod', 'production'],
272+
optional: Boolean,
273+
otp: [null, String],
274+
package: [String, Array],
275+
'package-lock': Boolean,
276+
'package-lock-only': Boolean,
277+
parseable: Boolean,
278+
'prefer-offline': Boolean,
279+
'prefer-online': Boolean,
280+
prefix: path,
281+
preid: String,
282+
production: Boolean,
283+
progress: Boolean,
284+
proxy: [null, false, url], // allow proxy to be disabled explicitly
285+
'read-only': Boolean,
286+
'rebuild-bundle': Boolean,
287+
registry: [null, url],
288+
rollback: Boolean,
289+
save: Boolean,
290+
'save-bundle': Boolean,
291+
'save-dev': Boolean,
292+
'save-exact': Boolean,
293+
'save-optional': Boolean,
294+
'save-prefix': String,
295+
'save-prod': Boolean,
296+
scope: String,
297+
'script-shell': [null, String],
298+
'scripts-prepend-node-path': [Boolean, 'auto', 'warn-only'],
299+
searchopts: String,
300+
searchexclude: [null, String],
301+
searchlimit: Number,
302+
searchstaleness: Number,
303+
'send-metrics': Boolean,
304+
shell: String,
305+
shrinkwrap: Boolean,
306+
'sign-git-commit': Boolean,
307+
'sign-git-tag': Boolean,
308+
'sso-poll-frequency': Number,
309+
'sso-type': [null, 'oauth', 'saml'],
310+
'strict-ssl': Boolean,
311+
tag: String,
312+
timing: Boolean,
313+
tmp: path,
314+
unicode: Boolean,
315+
'update-notifier': Boolean,
316+
usage: Boolean,
317+
userconfig: path,
318+
umask: Umask,
319+
version: Boolean,
320+
'tag-version-prefix': String,
321+
versions: Boolean,
322+
viewer: String
323+
}
324+
325+
const shorthands = {
326+
a: ['--all'],
327+
'enjoy-by': ['--before'],
328+
c: ['--call'],
329+
s: ['--loglevel', 'silent'],
330+
d: ['--loglevel', 'info'],
331+
dd: ['--loglevel', 'verbose'],
332+
ddd: ['--loglevel', 'silly'],
333+
noreg: ['--no-registry'],
334+
N: ['--no-registry'],
335+
reg: ['--registry'],
336+
'no-reg': ['--no-registry'],
337+
silent: ['--loglevel', 'silent'],
338+
verbose: ['--loglevel', 'verbose'],
339+
quiet: ['--loglevel', 'warn'],
340+
q: ['--loglevel', 'warn'],
341+
h: ['--usage'],
342+
H: ['--usage'],
343+
'?': ['--usage'],
344+
help: ['--usage'],
345+
v: ['--version'],
346+
f: ['--force'],
347+
desc: ['--description'],
348+
'no-desc': ['--no-description'],
349+
local: ['--no-global'],
350+
l: ['--long'],
351+
m: ['--message'],
352+
p: ['--parseable'],
353+
porcelain: ['--parseable'],
354+
readonly: ['--read-only'],
355+
g: ['--global'],
356+
S: ['--save'],
357+
D: ['--save-dev'],
358+
E: ['--save-exact'],
359+
O: ['--save-optional'],
360+
P: ['--save-prod'],
361+
y: ['--yes'],
362+
n: ['--no-yes'],
363+
B: ['--save-bundle'],
364+
C: ['--prefix']
365+
}
366+
367+
module.exports = { defaults, types, shorthands }

0 commit comments

Comments
 (0)