-
Notifications
You must be signed in to change notification settings - Fork 21
/
parseJsonReports.ts
54 lines (47 loc) · 1.68 KB
/
parseJsonReports.ts
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
import { readFile } from "node:fs/promises";
import path from "node:path";
import * as core from "@actions/core";
import { stripIndent } from "common-tags";
import type { JsonFinal } from "../types/JsonFinal";
import type { JsonSummary } from "../types/JsonSummary";
const parseVitestCoverageReport = async <type extends JsonSummary | JsonFinal>(
jsonPath: string,
): Promise<type> => {
const resolvedJsonSummaryPath = path.resolve(process.cwd(), jsonPath);
const jsonSummaryRaw = await readFile(resolvedJsonSummaryPath);
return JSON.parse(jsonSummaryRaw.toString()) as type;
};
const parseVitestJsonSummary = async (
jsonSummaryPath: string,
): Promise<JsonSummary> => {
try {
return await parseVitestCoverageReport<JsonSummary>(jsonSummaryPath);
} catch (err: unknown) {
const stack = err instanceof Error ? err.stack : "";
core.setFailed(stripIndent`
Failed to parse the json-summary at path "${jsonSummaryPath}."
Make sure to run vitest before this action and to include the "json-summary" reporter.
Original Error:
${stack}
`);
// Rethrow to abort the entire workflow
throw err;
}
};
const parseVitestJsonFinal = async (
jsonFinalPath: string,
): Promise<JsonFinal> => {
try {
return await parseVitestCoverageReport<JsonFinal>(jsonFinalPath);
} catch (err: unknown) {
const stack = err instanceof Error ? err.stack : "";
core.warning(stripIndent`
Failed to parse JSON Final at path "${jsonFinalPath}".
Line coverage will be empty. To include it, make sure to include the "json" reporter in your vitest execution.
Original Error:
${stack}
`);
return {};
}
};
export { parseVitestJsonSummary, parseVitestJsonFinal };