codekingpro/portable-devtools
114k
1const definitions = require('./definitions.js')
2
3// use the defined flattening function, and copy over any scoped
4// registries and registry-specific "nerfdart" configs verbatim
5//
6// TODO: make these getters so that we only have to make dirty
7// the thing that changed, and then flatten the fields that
8// could have changed when a config.set is called.
9//
10// TODO: move nerfdart auth stuff into a nested object that
11// is only passed along to paths that end up calling npm-registry-fetch.
12const flatten = (obj, flat = {}) => {
13 for (const [key, val] of Object.entries(obj)) {
14 const def = definitions[key]
15 if (def && def.flatten) {
16 def.flatten(key, obj, flat)
17 } else if (/@.*:registry$/i.test(key) || /^\/\//.test(key)) {
18 flat[key] = val
19 }
20 }
21 return flat
22}
23
24const definitionProps = Object.entries(definitions)
25 .reduce((acc, [key, { short = [], default: d }]) => {
26 // can be either an array or string
27 for (const s of [].concat(short)) {
28 acc.shorthands[s] = [`--${key}`]
29 }
30 acc.defaults[key] = d
31 return acc
32 }, { shorthands: {}, defaults: {} })
33
34// aliases where they get expanded into a completely different thing
35// these are NOT supported in the environment or npmrc files, only
36// expanded on the CLI.
37// TODO: when we switch off of nopt, use an arg parser that supports
38// more reasonable aliasing and short opts right in the definitions set.
39const shorthands = {
40 'enjoy-by': ['--before'],
41 d: ['--loglevel', 'info'],
42 dd: ['--loglevel', 'verbose'],
43 ddd: ['--loglevel', 'silly'],
44 quiet: ['--loglevel', 'warn'],
45 q: ['--loglevel', 'warn'],
46 s: ['--loglevel', 'silent'],
47 silent: ['--loglevel', 'silent'],
48 verbose: ['--loglevel', 'verbose'],
49 desc: ['--description'],
50 help: ['--usage'],
51 local: ['--no-global'],
52 n: ['--no-yes'],
53 no: ['--no-yes'],
54 porcelain: ['--parseable'],
55 readonly: ['--read-only'],
56 reg: ['--registry'],
57 iwr: ['--include-workspace-root'],
58 ws: ['--workspaces'],
59 ...definitionProps.shorthands,
60}
61
62// These are the configs that we can nerf-dart. Only _auth even has a config definition so we have to explicitly validate them here.
63// This is used to validate during "npm config set" and to not warn on loading unknown configs when we see these.
64const nerfDarts = [
65 '_auth', // Has a config
66 '_authToken', // Does not have a config
67 '_password', // Does not have a config
68 'certfile', // Does not have a config
69 'email', // Does not have a config
70 'keyfile', // Does not have a config
71 'username', // Does not have a config
72]
73
74const proxyEnv = [
75 'http_proxy',
76 'https_proxy',
77 'proxy',
78 'no_proxy',
79]
80
81module.exports = {
82 defaults: definitionProps.defaults,
83 definitions,
84 flatten,
85 nerfDarts,
86 proxyEnv,
87 shorthands,
88}
89 