-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli.js
251 lines (197 loc) · 7.29 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env node
const path = require('path');
const EC = require('eight-colors');
const { program } = require('commander');
const { foregroundChild } = require('foreground-child');
const MCR = require('./index.js');
const Util = require('./utils/util.js');
const version = require('../package.json').version;
const getRegisterPath = (filename) => {
const rel = Util.relativePath(path.resolve(__dirname, 'register', filename));
if (rel.startsWith('.')) {
return rel;
}
return `./${rel}`;
};
const getInitNodeOptions = async (cliOptions) => {
const nodeOptions = [];
if (process.env.NODE_OPTIONS) {
nodeOptions.push(process.env.NODE_OPTIONS);
}
if (cliOptions.import) {
nodeOptions.push(`--import ${cliOptions.import}`);
// for load mcr.config.ts
await import(cliOptions.import);
} else if (cliOptions.require) {
nodeOptions.push(`--require ${cliOptions.require}`);
// for load mcr.config.ts
await import(cliOptions.require);
}
return nodeOptions;
};
const getPreloadType = (nodeOptions) => {
const hasImport = nodeOptions.find((it) => it.includes('--import'));
if (hasImport) {
return '--import';
}
return '--require';
};
const checkRegisterFeature = () => {
const nv = process.versions.node;
// "module.register" added in Node.js: v20.6.0
// if (Util.cmpVersion(nv, '20.6.0') >= 0) {
// return true;
// }
// but also added in: v18.19.0
const requiredNV = '18.19.0';
if (Util.cmpVersion(nv, requiredNV) < 0) {
Util.logInfo(`The current Node.js version "${nv}" does NOT support "module.register", it requires "${requiredNV}" or higher.`);
return false;
}
// could be < 20.6.0 but just ignore it, please using latest minor version
return true;
};
const loadEnv = (cliOptions) => {
if (!cliOptions.env) {
return;
}
const envFile = cliOptions.env === true ? '.env' : cliOptions.env;
const loadEnvFile = process.loadEnvFile;
if (typeof loadEnvFile === 'function') {
loadEnvFile(envFile);
}
};
const initNodeOptions = async (cliOptions) => {
loadEnv(cliOptions);
const supportRegister = checkRegisterFeature();
if (!supportRegister) {
return;
}
const nodeOptions = await getInitNodeOptions(cliOptions);
// console.log(nodeOptions);
const preloadType = getPreloadType(nodeOptions);
// export source after
if (preloadType === '--import') {
const importPath = getRegisterPath('register.mjs');
nodeOptions.push(`--import ${importPath}`);
} else {
const requirePath = getRegisterPath('register.js');
nodeOptions.push(`--require ${requirePath}`);
}
// console.log(nodeOptions);
const nodeOptionsStr = nodeOptions.join(' ');
Util.logDebug(`node options: ${EC.cyan(nodeOptionsStr)}`);
process.env.NODE_OPTIONS = nodeOptionsStr;
};
const initNodeV8CoverageDir = (coverageOptions) => {
// dir for node v8 coverage
const nodeV8CoverageDir = Util.relativePath(path.resolve(coverageOptions.outputDir, '.v8-coverage'));
process.env.NODE_V8_COVERAGE = nodeV8CoverageDir;
// clean v8 cache before running
Util.rmSync(nodeV8CoverageDir);
// Util.logInfo(`V8 coverage dir: ${EC.cyan(nodeV8CoverageDir)}`);
return nodeV8CoverageDir;
};
const mergeCoverage = async (cliOptions) => {
const coverageReport = MCR(cliOptions);
await coverageReport.loadConfig(cliOptions.config);
coverageReport.cleanCache();
await coverageReport.generate();
};
const executeCommand = async (command, cliOptions) => {
Util.logInfo(`Execute: ${EC.cyan(command)}`);
if (command === 'merge') {
return mergeCoverage(cliOptions);
}
// before load config
await initNodeOptions(cliOptions);
// console.log(options);
const coverageReport = MCR(cliOptions);
await coverageReport.loadConfig(cliOptions.config);
coverageReport.cleanCache();
const coverageOptions = coverageReport.options;
const nodeV8CoverageDir = initNodeV8CoverageDir(coverageOptions);
// =========================================
// onStart hook
const onStart = coverageOptions.onStart;
if (typeof onStart === 'function') {
await onStart(coverageReport);
}
// =========================================
const subprocess = foregroundChild(command, {
shell: true
}, async (code, signal) => {
// generate coverage even it is failed. code != 0
// =========================================
// onReady hook before adding coverage data.
// Sometimes, the child process has not yet finished writing the coverage data, and it needs to wait here.
const onReady = coverageOptions.onReady;
if (typeof onReady === 'function') {
await onReady(coverageReport, nodeV8CoverageDir, subprocess);
}
// =========================================
await coverageReport.addFromDir(nodeV8CoverageDir);
await coverageReport.generate();
// remove nodeV8CoverageDir
if (!Util.isDebug()) {
Util.rmSync(nodeV8CoverageDir);
}
return process.exitCode;
});
};
process.on('uncaughtException', function(err) {
Util.logError(`Process uncaughtException: ${err.message}`);
console.log(err.stack);
});
// the -- separator
const argv = [];
const subArgv = [];
let separator = false;
process.argv.forEach((it) => {
if (!separator && it === '--') {
separator = true;
}
if (separator) {
subArgv.push(it);
} else {
argv.push(it);
}
});
program
.name('mcr')
.description('CLI to generate coverage reports')
.version(version, '-v, --version', 'output the current version')
.argument('[command]', 'command to execute')
.allowUnknownOption()
.allowExcessArguments()
.option('-c, --config <path>', 'custom config file path')
.option('-l, --logging <logging>', 'off, error, info, debug')
.option('-n, --name <name>', 'report name for title')
.option('-r, --reports <name[,name]>', 'coverage reports to use')
.option('-o, --outputDir <dir>', 'output dir for reports')
.option('-i, --inputDir <dir>', 'input dir for merging raw files')
.option('-b, --baseDir <dir>', 'base dir for normalizing path')
.option('-a, --all <dir>', 'include all files from dir')
.option('--entryFilter <pattern>', 'entry url filter')
.option('--sourceFilter <pattern>', 'source path filter')
.option('--filter <pattern>', 'the combined filter')
.option('--outputFile <path>', 'output file for v8 report')
.option('--inline', 'inline html for v8 report')
.option('--assetsPath <path>', 'assets path if not inline')
.option('--lcov', 'generate lcov.info file')
.option('--import <module>', 'preload module at startup')
.option('--require <module>', 'preload module at startup')
.option('--env [path]', 'env file (default: ".env")')
.action((_command, cliOptions) => {
const args = [].concat(program.args).concat(subArgv);
if (args[0] === '--') {
args.shift();
}
const command = args.join(' ').trim();
if (!command) {
program.outputHelp();
return;
}
executeCommand(command, cliOptions);
});
program.parse(argv);