• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
22  // XXX make this the bin/npm-cli.js file explicitly instead
23  // otherwise using npm programmatically is a bit of a pain.
24  flat.npmBin = require.main ? require.main.filename
25    : /* istanbul ignore next - not configurable property */ undefined
26  flat.nodeBin = process.env.NODE || process.execPath
27
28  // XXX should this be sha512?  is it even relevant?
29  flat.hashAlgorithm = 'sha1'
30
31  return flat
32}
33
34const definitionProps = Object.entries(definitions)
35  .reduce((acc, [key, { short = [], default: d }]) => {
36  // can be either an array or string
37    for (const s of [].concat(short)) {
38      acc.shorthands[s] = [`--${key}`]
39    }
40    acc.defaults[key] = d
41    return acc
42  }, { shorthands: {}, defaults: {} })
43
44// aliases where they get expanded into a completely different thing
45// these are NOT supported in the environment or npmrc files, only
46// expanded on the CLI.
47// TODO: when we switch off of nopt, use an arg parser that supports
48// more reasonable aliasing and short opts right in the definitions set.
49const shorthands = {
50  'enjoy-by': ['--before'],
51  d: ['--loglevel', 'info'],
52  dd: ['--loglevel', 'verbose'],
53  ddd: ['--loglevel', 'silly'],
54  quiet: ['--loglevel', 'warn'],
55  q: ['--loglevel', 'warn'],
56  s: ['--loglevel', 'silent'],
57  silent: ['--loglevel', 'silent'],
58  verbose: ['--loglevel', 'verbose'],
59  desc: ['--description'],
60  help: ['--usage'],
61  local: ['--no-global'],
62  n: ['--no-yes'],
63  no: ['--no-yes'],
64  porcelain: ['--parseable'],
65  readonly: ['--read-only'],
66  reg: ['--registry'],
67  iwr: ['--include-workspace-root'],
68  ...definitionProps.shorthands,
69}
70
71module.exports = {
72  defaults: definitionProps.defaults,
73  definitions,
74  flatten,
75  shorthands,
76}
77