|
1 |
| -const options = module.exports = { |
2 |
| - path: undefined, |
3 |
| - cache: `${process.env.HOME}/.npm/_cacache`, |
4 |
| - _: [], |
| 1 | +const nopt = require('nopt') |
| 2 | +const path = require('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] |
5 | 11 | }
|
6 | 12 |
|
7 |
| -for (const arg of process.argv.slice(2)) { |
8 |
| - if (/^--add=/.test(arg)) { |
9 |
| - options.add = options.add || [] |
10 |
| - options.add.push(arg.substr('--add='.length)) |
11 |
| - } else if (/^--rm=/.test(arg)) { |
12 |
| - options.rm = options.rm || [] |
13 |
| - options.rm.push(arg.substr('--rm='.length)) |
14 |
| - } else if (arg === '--global') { |
15 |
| - options.global = true |
16 |
| - } else if (arg === '--global-style') { |
17 |
| - options.globalStyle = true |
18 |
| - } else if (arg === '--prefer-dedupe') { |
19 |
| - options.preferDedupe = true |
20 |
| - } else if (arg === '--legacy-peer-deps') { |
21 |
| - options.legacyPeerDeps = true |
22 |
| - } else if (arg === '--force') { |
23 |
| - options.force = true |
24 |
| - } else if (arg === '--update-all') { |
25 |
| - options.update = options.update || {} |
26 |
| - options.update.all = true |
27 |
| - } else if (/^--update=/.test(arg)) { |
28 |
| - options.update = options.update || {} |
29 |
| - options.update.names = options.update.names || [] |
30 |
| - options.update.names.push(arg.substr('--update='.length)) |
31 |
| - } else if (/^--omit=/.test(arg)) { |
32 |
| - options.omit = options.omit || [] |
33 |
| - options.omit.push(arg.substr('--omit='.length)) |
34 |
| - } else if (/^--before=/.test(arg)) { |
35 |
| - options.before = new Date(arg.substr('--before='.length)) |
36 |
| - } else if (/^-w.+/.test(arg)) { |
37 |
| - options.workspaces = options.workspaces || [] |
38 |
| - options.workspaces.push(arg.replace(/^-w/, '')) |
39 |
| - } else if (/^--workspace=/.test(arg)) { |
40 |
| - options.workspaces = options.workspaces || [] |
41 |
| - options.workspaces.push(arg.replace(/^--workspace=/, '')) |
42 |
| - } else if (/^--[^=]+=/.test(arg)) { |
43 |
| - const [key, ...v] = arg.replace(/^--/, '').split('=') |
44 |
| - const val = v.join('=') |
45 |
| - options[key] = val === 'false' ? false : val === 'true' ? true : val |
46 |
| - } else if (/^--.+/.test(arg)) { |
47 |
| - options[arg.replace(/^--/, '')] = true |
48 |
| - } else if (options.path === undefined) { |
49 |
| - options.path = arg |
50 |
| - } else { |
51 |
| - options._.push(arg) |
| 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) |
52 | 118 | }
|
53 |
| -} |
54 | 119 |
|
55 |
| -if (options.path === undefined) { |
56 |
| - options.path = '.' |
| 120 | + return options |
57 | 121 | }
|
58 | 122 |
|
59 |
| -console.error(options) |
| 123 | +module.exports = parse() |
0 commit comments