-
Notifications
You must be signed in to change notification settings - Fork 19
/
parseCoverageThresholds.ts
52 lines (45 loc) · 1.46 KB
/
parseCoverageThresholds.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
import { promises as fs } from "node:fs";
import path from "node:path";
import * as core from "@actions/core";
import type { Thresholds } from "../types/Threshold";
const regex100 = /100"?\s*:\s*true/;
const regexStatements = /statements\s*:\s*(\d+)/;
const regexLines = /lines:\s*(\d+)/;
const regexBranches = /branches\s*:\s*(\d+)/;
const regexFunctions = /functions\s*:\s*(\d+)/;
const parseCoverageThresholds = async (
vitestConfigPath: string,
): Promise<Thresholds> => {
try {
const resolvedViteConfigPath = path.resolve(
process.cwd(),
vitestConfigPath,
);
const rawContent = await fs.readFile(resolvedViteConfigPath, "utf8");
const has100Value = rawContent.match(regex100);
if (has100Value) {
return {
lines: 100,
branches: 100,
functions: 100,
statements: 100,
};
}
const lines = rawContent.match(regexLines);
const branches = rawContent.match(regexBranches);
const functions = rawContent.match(regexFunctions);
const statements = rawContent.match(regexStatements);
return {
lines: lines ? Number.parseInt(lines[1]) : undefined,
branches: branches ? Number.parseInt(branches[1]) : undefined,
functions: functions ? Number.parseInt(functions[1]) : undefined,
statements: statements ? Number.parseInt(statements[1]) : undefined,
};
} catch (err: unknown) {
core.warning(
`Could not read vite config file for thresholds due to an error:\n ${err}`,
);
return {};
}
};
export { parseCoverageThresholds };