-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (57 loc) · 1.47 KB
/
index.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
const Fs = require('fs');
const Path = require('path');
// ----------
// USAGE
// ----------
function usage(config, argvIndex = 2) {
return Promise.resolve().then(() => {
const argv = process.argv[argvIndex];
if (!argv || !config[argv]) {
const prefix = process.argv.slice(2, argvIndex).reduce((acc, cur) => {
acc.push(cur);
return acc;
}, [
'node',
Fs.lstatSync(process.argv[1]).isDirectory()
? 'index'
: Path.basename(process.argv[1], Path.extname(process.argv[1]))
]).join(' ');
const commands = Object.keys(config).map((key) => {
const value = config[key];
return [
[
key,
...typeof value !== 'object' || !value.options
? []
: !Array.isArray(value.options)
? [value.options]
: value.options
].join(' '),
typeof value !== 'object' || !value.description
? ''
: value.description
];
});
const padding = Math.max(...commands.map((command) => {
return command[0].length;
})) + 3;
console.group('Usage:');
for (const command of commands) {
console.log(prefix, command[0].padEnd(padding), command[1]);
}
console.groupEnd();
console.log();
return;
}
const callback = typeof config[argv] !== 'object'
? config[argv]
: config[argv].callback || ((nextAgvIndex) => {
return usage(config[argv].usage, nextAgvIndex);
});
return callback(argvIndex + 1);
});
}
// ----------
// EXPORTS
// ----------
module.exports = usage;