-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoverage-node.mjs
executable file
·51 lines (43 loc) · 1.73 KB
/
coverage-node.mjs
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
#!/usr/bin/env node
// @ts-check
import disposableDirectory from "disposable-directory";
import { spawn } from "node:child_process";
import analyseCoverage from "./analyseCoverage.mjs";
import childProcessPromise from "./childProcessPromise.mjs";
import CliError from "./CliError.mjs";
import reportCliError from "./reportCliError.mjs";
import reportCoverage from "./reportCoverage.mjs";
/**
* Powers the `coverage-node` CLI. Runs Node.js with the given arguments and
* coverage enabled. An analysis of the coverage is reported to the console, and
* if coverage is incomplete and there isn’t a truthy `ALLOW_MISSING_COVERAGE`
* environment variable the process exits with code `1`.
* @returns {Promise<void>} Resolves when all work is complete.
*/
async function coverageNode() {
try {
const [, , ...nodeArgs] = process.argv;
if (!nodeArgs.length)
throw new CliError("Node.js CLI arguments are required.");
await disposableDirectory(async (tempDirPath) => {
const { exitCode } = await childProcessPromise(
spawn("node", nodeArgs, {
stdio: "inherit",
env: { ...process.env, NODE_V8_COVERAGE: tempDirPath },
})
);
// Only show a code coverage report if the Node.js script didn’t error,
// to reduce distraction from the priority to solve errors.
if (exitCode === 0) {
const analysis = await analyseCoverage(tempDirPath);
reportCoverage(analysis);
if (analysis.uncovered.length && !process.env.ALLOW_MISSING_COVERAGE)
process.exitCode = 1;
} else if (exitCode !== null) process.exitCode = exitCode;
});
} catch (error) {
reportCliError("Node.js with coverage", error);
process.exitCode = 1;
}
}
coverageNode();