-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path@cli.js
109 lines (83 loc) · 2.2 KB
/
@cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const Path = require('path');
const Chalk = require('chalk');
const HELP = `
${Chalk.green('USAGE')}
nodemand [options] <module-path> [...args]
${Chalk.green('OPTIONS')}
--debounce <delay> Debounce restart after change detection, defaults to 1000.
--node-modules Watch also files under node_modules (symbolic links will
be resolved before filtering).
--help Show this help content.
`;
const OPTION_DEFINITION_MAP = new Map([
[
'debounce',
{
default: 1000,
cast: raw => {
const value = Number(raw);
if (isNaN(value)) {
throw `expecting a number but got ${JSON.stringify(raw)}`;
}
return value;
},
},
],
['node-modules', true],
['color', true],
['no-color', true],
]);
const execArgv = [];
const parsedOptionMap = new Map();
const args = process.argv.slice(2);
while (args.length) {
const [arg] = args;
if (!arg.startsWith('--')) {
break;
}
const option = args.shift().slice(2);
if (option === 'help') {
console.info(HELP);
process.exit();
}
const definition = OPTION_DEFINITION_MAP.get(option);
if (!definition) {
// Unknown option to nodemand, forward to Node.js.
execArgv.push(arg);
continue;
}
if (definition === true) {
parsedOptionMap.set(option, true);
} else {
if (!args.length) {
console.error(Chalk.red(`Expecting value for option "${option}"`));
process.exit(1);
}
const raw = args.shift();
let value;
try {
value = definition.cast(raw);
} catch (error) {
console.error(Chalk.red(`Error casting option "${option}": ${error}`));
process.exit(1);
}
parsedOptionMap.set(option, value);
}
}
const optionDict = Object.create(null);
for (const [option, definition] of OPTION_DEFINITION_MAP) {
optionDict[option] = parsedOptionMap.has(option)
? parsedOptionMap.get(option)
: definition === true
? false
: definition.default;
}
if (!args.length) {
console.error(HELP);
process.exit(1);
}
const [modulePath, ...restArgs] = args;
exports.execArgv = execArgv;
exports.options = optionDict;
exports.modulePath = Path.resolve(modulePath);
exports.args = restArgs;