-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.ts
More file actions
156 lines (140 loc) · 4.46 KB
/
index.ts
File metadata and controls
156 lines (140 loc) · 4.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import type { RunnerConfig, RunnerFilesPaths } from '@code-pushup/models';
import {
createRunnerFiles,
ensureDirectoryExists,
executeProcess,
isPromiseFulfilledResult,
isPromiseRejectedResult,
objectFromEntries,
objectToCliArgs,
readJsonFile,
} 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 async function createRunnerConfig(
scriptPath: string,
config: FinalJSPackagesPluginConfig,
): Promise<RunnerConfig> {
const { runnerConfigPath, runnerOutputPath } = await createRunnerFiles(
'js-packages',
JSON.stringify(config),
);
return {
command: 'node',
args: [
scriptPath,
...objectToCliArgs({ runnerConfigPath, runnerOutputPath }),
],
configFile: runnerConfigPath,
outputFile: runnerOutputPath,
};
}
export async function executeRunner({
runnerConfigPath,
runnerOutputPath,
}: RunnerFilesPaths): Promise<void> {
const {
packageManager,
checks,
auditLevelMapping,
packageJsonPath,
dependencyGroups: depGroups,
} = await readJsonFile<FinalJSPackagesPluginConfig>(runnerConfigPath);
const auditResults = checks.includes('audit')
? await processAudit(
packageManager,
depGroups,
auditLevelMapping,
packageJsonPath,
)
: [];
const outdatedResults = checks.includes('outdated')
? await processOutdated(packageManager, depGroups, packageJsonPath)
: [];
const checkResults = [...auditResults, ...outdatedResults];
await ensureDirectoryExists(path.dirname(runnerOutputPath));
await writeFile(runnerOutputPath, JSON.stringify(checkResults));
}
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 Promise.allSettled(
compatibleAuditDepGroups.map(
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 rejected = auditResults.filter(isPromiseRejectedResult);
if (rejected.length > 0) {
rejected.forEach(result => {
console.error(result.reason);
});
throw new Error(`JS Packages plugin: Running ${pm.name} audit failed.`);
}
const fulfilled = objectFromEntries(
auditResults.filter(isPromiseFulfilledResult).map(x => x.value),
);
const uniqueResults = pm.audit.postProcessResult?.(fulfilled) ?? fulfilled;
return compatibleAuditDepGroups.map(depGroup =>
auditResultToAuditOutput(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
uniqueResults[depGroup]!,
id,
depGroup,
auditLevelMapping,
),
);
}