1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | import { spawn } from "child_process";
|
5 | import disposableDirectory from "disposable-directory";
|
6 | import { yellow } from "kleur/colors";
|
7 |
|
8 | import analyseCoverage from "./analyseCoverage.mjs";
|
9 | import childProcessPromise from "./childProcessPromise.mjs";
|
10 | import CliError from "./CliError.mjs";
|
11 | import coverageSupported from "./coverageSupported.mjs";
|
12 | import minNodeVersion from "./coverageSupportedMinNodeVersion.mjs";
|
13 | import reportCliError from "./reportCliError.mjs";
|
14 | import reportCoverage from "./reportCoverage.mjs";
|
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.env.ALLOW_MISSING_COVERAGE)
|
46 | process.exitCode = 1;
|
47 | } else if (exitCode !== null) process.exitCode = exitCode;
|
48 | });
|
49 |
|
50 | } else {
|
51 | const { exitCode } = await childProcessPromise(
|
52 | spawn("node", nodeArgs, { stdio: "inherit" })
|
53 | );
|
54 |
|
55 | if (exitCode === 0)
|
56 | console.info(
|
57 | `\n${yellow(
|
58 | `Skipped code coverage as Node.js is ${process.version}, v${minNodeVersion.major}.${minNodeVersion.minor}.${minNodeVersion.patch}+ is supported.`
|
59 | )}\n`
|
60 | );
|
61 | else if (exitCode !== null) process.exitCode = exitCode;
|
62 | }
|
63 | } catch (error) {
|
64 | reportCliError(
|
65 | `Node.js${
|
66 | // coverage ignore next line
|
67 | coverageSupported ? " with coverage" : ""
|
68 | }`,
|
69 | error
|
70 | );
|
71 |
|
72 | process.exitCode = 1;
|
73 | }
|
74 | }
|
75 |
|
76 | coverageNode();
|