-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
72 lines (58 loc) · 1.67 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
import {execSync} from 'child_process';
import tryCatch from 'try-catch';
import yargsParser from 'yargs-parser';
import {version} from './version.js';
import reportLines from '../formatters/lines.js';
import reportFiles from '../formatters/files.js';
const {ESCOVER_FORMAT} = process.env;
export const ESCOVER_NODE_OPTIONS = '--no-warnings --loader zenload';
// ${NODE_OPTIONS}'`;
export const cli = ({argv, exit, readCoverage}) => {
const args = yargsParser(argv.slice(2), {
string: [
'format',
],
boolean: [
'version',
],
alias: {
v: 'version',
f: 'format',
},
default: {
format: ESCOVER_FORMAT || 'files',
},
});
if (args.version) {
console.log(`v${version()}`);
return exit();
}
const cmd = argv.slice(2);
if (cmd.length) {
execute(`"${cmd.join('" "')}"`, exit);
}
const coverage = readCoverage();
let output = '';
if (args.format === 'lines')
output = reportLines(coverage);
else
output = reportFiles(coverage);
process.stdout.write(output);
};
export const isSuccess = (error) => error?.status === Number(process.env.ESCOVER_SUCCESS_EXIT_CODE);
function execute(cmd, exit) {
const [error] = tryCatch(execSync, cmd, {
stdio: [0, 1, 2],
env: {
ZENLOAD: 'escover,mock-import',
...process.env,
NODE_OPTIONS: ESCOVER_NODE_OPTIONS,
},
});
if (isSuccess(error))
return exit(0);
if (error) {
console.error(error.message);
return exit(1);
}
}