-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathcml.js
executable file
·68 lines (61 loc) · 1.62 KB
/
cml.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
#!/usr/bin/env node
const { basename } = require('path');
const { pseudoexec } = require('pseudoexec');
const which = require('which');
const winston = require('winston');
const yargs = require('yargs');
const configureLogger = (level) => {
winston.configure({
format: process.stdout.isTTY
? winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.simple()
)
: winston.format.json(),
transports: [
new winston.transports.Console({
handleExceptions: true,
handleRejections: true,
level
})
]
});
};
const runPlugin = async ({ $0: executable, command }) => {
try {
if (command === undefined) throw new Error('no command');
const path = which.sync(`${basename(executable)}-${command}`);
const parameters = process.argv.slice(process.argv.indexOf(command) + 1); // HACK
process.exit(await pseudoexec(path, parameters));
} catch (error) {
yargs.showHelp();
winston.debug(error);
}
};
const handleError = (message, error) => {
if (error) {
winston.error(error);
} else {
yargs.showHelp();
console.error('\n' + message);
}
process.exit(1);
};
const options = {
log: {
describe: 'Maximum log level',
coerce: (value) => configureLogger(value) && value,
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
}
};
yargs
.fail(handleError)
.env('CML')
.options(options)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command('$0 <command>', false, (builder) => builder.strict(false), runPlugin)
.recommendCommands()
.demandCommand()
.strict()
.parse();