-
Notifications
You must be signed in to change notification settings - Fork 20
/
parseJsonReports.ts
50 lines (42 loc) · 1.58 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
import { readFile } from 'node:fs/promises';
import * as core from '@actions/core';
import path from 'node:path';
import { JsonFinal } from './types/JsonFinal';
import { JsonSummary } from './types/JsonSummary';
import { stripIndent } from 'common-tags';
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: any) {
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:
${err.stack}
`);
// Rethrow to abort the entire workflow
throw err;
}
}
const parseVitestJsonFinal = async (jsonFinalPath: string): Promise<JsonFinal> => {
try {
return await parseVitestCoverageReport<JsonFinal>(jsonFinalPath);
} catch (err: any) {
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:
${err.stack}
`);
return {};
}
};
export {
parseVitestJsonSummary,
parseVitestJsonFinal,
};