-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·43 lines (37 loc) · 921 Bytes
/
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
#!/usr/bin/env node
const { join } = require('path');
const [,, ...args] = process.argv;
const argv = require('yargs-parser')(args);
const { name, version } = require('./package.json');
process.on('unhandledRejection', console.error);
/**
* Entry point of CLI application
*/
(async() => {
try {
if (argv.v || argv.version) {
console.log(name, version);
return;
}
const program = getProgram(argv);
const result = await program(argv);
result && console.log(result);
} catch (error) {
console.error(error);
process.exit(1);
}
})();
function getProgram({_: [program]}) {
try {
const route = join(__dirname, `programs/${program}/cli.js`);
return require(route);
} catch (error) {
if (`${error.message}`.toLowerCase().includes('cannot find module')) {
console.error(`Can not find program "${program}"`);
console.error(error);
process.exit(1);
} else {
throw error;
}
}
}