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  ...definitionProps.shorthands,
59}
60
61module.exports = {
62  defaults: definitionProps.defaults,
63  definitions,
64  flatten,
65  shorthands,
66}
67