-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.ts
81 lines (73 loc) · 2.37 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { bold } from 'ansis';
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import type {
AuditOutputs,
RunnerConfig,
RunnerFilesPaths,
} from '@code-pushup/models';
import {
ProcessError,
createRunnerFiles,
ensureDirectoryExists,
executeProcess,
filePathToCliArg,
objectToCliArgs,
readJsonFile,
ui,
} from '@code-pushup/utils';
import type { FinalCoveragePluginConfig } from '../config.js';
import { applyMaxScoreAboveThreshold } from '../utils.js';
import { lcovResultsToAuditOutputs } from './lcov/lcov-runner.js';
export async function executeRunner({
runnerConfigPath,
runnerOutputPath,
}: RunnerFilesPaths): Promise<void> {
const { reports, coverageToolCommand, coverageTypes } =
await readJsonFile<FinalCoveragePluginConfig>(runnerConfigPath);
// Run coverage tool if provided
if (coverageToolCommand != null) {
const { command, args } = coverageToolCommand;
try {
await executeProcess({ command, args });
} catch (error) {
if (error instanceof ProcessError) {
ui().logger.error(bold('stdout from failed coverage tool process:'));
ui().logger.error(error.stdout);
ui().logger.error(bold('stderr from failed coverage tool process:'));
ui().logger.error(error.stderr);
}
throw new Error(
'Coverage plugin: Running coverage tool failed. Make sure all your provided tests are passing.',
);
}
}
// Calculate coverage from LCOV results
const auditOutputs = await lcovResultsToAuditOutputs(reports, coverageTypes);
await ensureDirectoryExists(path.dirname(runnerOutputPath));
await writeFile(runnerOutputPath, JSON.stringify(auditOutputs));
}
export async function createRunnerConfig(
scriptPath: string,
config: FinalCoveragePluginConfig,
): Promise<RunnerConfig> {
// Create JSON config for executeRunner
const { runnerConfigPath, runnerOutputPath } = await createRunnerFiles(
'coverage',
JSON.stringify(config),
);
const threshold = config.perfectScoreThreshold;
return {
command: 'node',
args: [
filePathToCliArg(scriptPath),
...objectToCliArgs({ runnerConfigPath, runnerOutputPath }),
],
configFile: runnerConfigPath,
outputFile: runnerOutputPath,
...(threshold != null && {
outputTransform: outputs =>
applyMaxScoreAboveThreshold(outputs as AuditOutputs, threshold),
}),
};
}