-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrunner.ts
More file actions
116 lines (105 loc) · 3.34 KB
/
Copy pathrunner.ts
File metadata and controls
116 lines (105 loc) · 3.34 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import path from 'node:path';
import type { RunnerFunction } from '@code-pushup/models';
import {
asyncSequential,
executeProcess,
objectFromEntries,
} from '@code-pushup/utils';
import {
type AuditSeverity,
type DependencyGroup,
type FinalJSPackagesPluginConfig,
type PackageJsonPath,
type PackageManagerId,
dependencyGroups,
} from '../config.js';
import { dependencyGroupToLong } from '../constants.js';
import { packageManagers } from '../package-managers/package-managers.js';
import { auditResultToAuditOutput } from './audit/transform.js';
import type { AuditResult } from './audit/types.js';
import { outdatedResultToAuditOutput } from './outdated/transform.js';
import { getTotalDependencies } from './utils.js';
export function createRunnerFunction(
config: FinalJSPackagesPluginConfig,
): RunnerFunction {
return async () => {
const {
packageManager,
checks,
auditLevelMapping,
packageJsonPath,
dependencyGroups: depGroups,
} = config;
const auditResults = checks.includes('audit')
? await processAudit(
packageManager,
depGroups,
auditLevelMapping,
packageJsonPath,
)
: [];
const outdatedResults = checks.includes('outdated')
? await processOutdated(packageManager, depGroups, packageJsonPath)
: [];
return [...auditResults, ...outdatedResults];
};
}
async function processOutdated(
id: PackageManagerId,
depGroups: DependencyGroup[],
packageJsonPath: PackageJsonPath,
) {
const pm = packageManagers[id];
const { stdout } = await executeProcess({
command: pm.command,
args: pm.outdated.commandArgs,
cwd: packageJsonPath ? path.dirname(packageJsonPath) : process.cwd(),
ignoreExitCode: true, // outdated returns exit code 1 when outdated dependencies are found
});
const depTotals = await getTotalDependencies(packageJsonPath);
const normalizedResult = pm.outdated.unifyResult(stdout);
return depGroups.map(depGroup =>
outdatedResultToAuditOutput(
normalizedResult,
id,
depGroup,
depTotals[dependencyGroupToLong[depGroup]],
),
);
}
async function processAudit(
id: PackageManagerId,
depGroups: DependencyGroup[],
auditLevelMapping: AuditSeverity,
packageJsonPath: PackageJsonPath,
) {
const pm = packageManagers[id];
const supportedAuditDepGroups =
pm.audit.supportedDepGroups ?? dependencyGroups;
const compatibleAuditDepGroups = depGroups.filter(group =>
supportedAuditDepGroups.includes(group),
);
const auditResults = await asyncSequential(
compatibleAuditDepGroups,
async (depGroup): Promise<[DependencyGroup, AuditResult]> => {
const { stdout } = await executeProcess({
command: pm.command,
args: pm.audit.getCommandArgs(depGroup),
cwd: packageJsonPath ? path.dirname(packageJsonPath) : process.cwd(),
ignoreExitCode: pm.audit.ignoreExitCode,
});
return [depGroup, pm.audit.unifyResult(stdout)];
},
);
const resultsMap = objectFromEntries(auditResults);
const uniqueResults = pm.audit.postProcessResult?.(resultsMap) ?? resultsMap;
return compatibleAuditDepGroups.map(depGroup =>
auditResultToAuditOutput(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
uniqueResults[depGroup]!,
id,
depGroup,
auditLevelMapping,
),
);
}