1 | #!/usr/bin/env node
|
2 |
|
3 | import { spawn } from 'child_process';
|
4 | import disposableDirectory from 'disposable-directory';
|
5 | import kleur from 'kleur';
|
6 | import CliError from '../private/CliError.mjs';
|
7 | import childProcessPromise from '../private/childProcessPromise.mjs';
|
8 | import reportCliError from '../private/reportCliError.mjs';
|
9 | import analyseCoverage from '../public/analyseCoverage.mjs';
|
10 | import coverageSupported from '../public/coverageSupported.mjs';
|
11 | import minNodeVersion from '../public/coverageSupportedMinNodeVersion.mjs';
|
12 | import reportCoverage from '../public/reportCoverage.mjs';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | async function coverageNode() {
|
24 | try {
|
25 | const [, , ...nodeArgs] = process.argv;
|
26 |
|
27 | if (!nodeArgs.length)
|
28 | throw new CliError('Node.js CLI arguments are required.');
|
29 |
|
30 |
|
31 | if (coverageSupported) {
|
32 | await disposableDirectory(async (tempDirPath) => {
|
33 | const { exitCode } = await childProcessPromise(
|
34 | spawn('node', nodeArgs, {
|
35 | stdio: 'inherit',
|
36 | env: { ...process.env, NODE_V8_COVERAGE: tempDirPath },
|
37 | })
|
38 | );
|
39 |
|
40 |
|
41 |
|
42 | if (exitCode === 0) {
|
43 | const analysis = await analyseCoverage(tempDirPath);
|
44 | reportCoverage(analysis);
|
45 | if (analysis.uncovered.length) process.exitCode = 1;
|
46 | } else process.exitCode = exitCode;
|
47 | });
|
48 |
|
49 | } else {
|
50 | const { exitCode } = await childProcessPromise(
|
51 | spawn('node', nodeArgs, { stdio: 'inherit' })
|
52 | );
|
53 |
|
54 | if (exitCode === 0)
|
55 | console.info(
|
56 | `\n${kleur.yellow(
|
57 | `Skipped code coverage as Node.js is ${process.version}, v${minNodeVersion.major}.${minNodeVersion.minor}.${minNodeVersion.patch}+ is supported.`
|
58 | )}\n`
|
59 | );
|
60 | else process.exitCode = exitCode;
|
61 | }
|
62 | } catch (error) {
|
63 | reportCliError(
|
64 | `Node.js${
|
65 | // coverage ignore next line
|
66 | coverageSupported ? ' with coverage' : ''
|
67 | }`,
|
68 | error
|
69 | );
|
70 |
|
71 | process.exitCode = 1;
|
72 | }
|
73 | }
|
74 |
|
75 | coverageNode();
|