Debian Codegraph @

workspaces/arborist/bin/lib/options.js

raw
1 const nopt = require('nopt')
2 const path = require('node:path')
3
4 const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k)
5
6 const cleanPath = (val) => {
7 const k = Symbol('key')
8 const data = {}
9 nopt.typeDefs.path.validate(data, k, val)
10 return data[k]
11 }
12
13 const parse = (...noptArgs) => {
14 const binOnlyOpts = {
15 command: String,
16 loglevel: String,
17 colors: Boolean,
18 timing: ['always', Boolean],
19 logfile: String,
20 }
21
22 const arbOpts = {
23 add: Array,
24 rm: Array,
25 omit: Array,
26 update: Array,
27 workspaces: Array,
28 global: Boolean,
29 force: Boolean,
30 'global-style': Boolean,
31 'prefer-dedupe': Boolean,
32 'legacy-peer-deps': Boolean,
33 'update-all': Boolean,
34 before: Date,
35 path: path,
36 cache: path,
37 ...binOnlyOpts,
38 }
39
40 const short = {
41 quiet: ['--loglevel', 'warn'],
42 logs: ['--logfile', 'true'],
43 w: '--workspaces',
44 g: '--global',
45 f: '--force',
46 }
47
48 const defaults = {
49 // key order is important for command and path
50 // since they shift positional args
51 // command is 1st, path is 2nd
52 command: (o) => o.argv.remain.shift(),
53 path: (o) => cleanPath(o.argv.remain.shift() || '.'),
54 colors: has(process.env, 'NO_COLOR') ? false : !!process.stderr.isTTY,
55 loglevel: 'silly',
56 timing: (o) => o.loglevel === 'silly',
57 cache: `${process.env.HOME}/.npm/_cacache`,
58 }
59
60 const derived = [
61 // making update either `all` or an array of names but not both
62 ({ updateAll: all, update: names, ...o }) => {
63 if (all || names) {
64 o.update = all != null ? { all } : { names }
65 }
66 return o
67 },
68 ({ logfile, ...o }) => {
69 // logfile is parsed as a string so if its true or set but empty
70 // then set the default logfile
71 if (logfile === 'true' || logfile === '') {
72 logfile = `arb-log-${new Date().toISOString().replace(/[.:]/g, '_')}.log`
73 }
74 // then parse it the same as nopt parses other paths
75 if (logfile) {
76 o.logfile = cleanPath(logfile)
77 }
78 return o
79 },
80 ]
81
82 const transforms = [
83 // Camelcase all top level keys
84 (o) => {
85 const entries = Object.entries(o).map(([k, v]) => [
86 k.replace(/-./g, s => s[1].toUpperCase()),
87 v,
88 ])
89 return Object.fromEntries(entries)
90 },
91 // Set defaults on unset keys
92 (o) => {
93 for (const [k, v] of Object.entries(defaults)) {
94 if (!has(o, k)) {
95 o[k] = typeof v === 'function' ? v(o) : v
96 }
97 }
98 return o
99 },
100 // Set/unset derived values
101 ...derived.map((derive) => (o) => derive(o) || o),
102 // Separate bin and arborist options
103 ({ argv: { remain: _ }, ...o }) => {
104 const bin = { _ }
105 for (const k of Object.keys(binOnlyOpts)) {
106 if (has(o, k)) {
107 bin[k] = o[k]
108 delete o[k]
109 }
110 }
111 return { bin, arb: o }
112 },
113 ]
114
115 let options = nopt(arbOpts, short, ...noptArgs)
116 for (const t of transforms) {
117 options = t(options)
118 }
119
120 return options
121 }
122
123 module.exports = parse()