返回 CodeWhale
audit-dependencies.mjs
根目录 / web / scripts / audit-dependencies.mjs
1 import { spawnSync } from 'node:child_process';
2 import { writeFileSync } from 'node:fs';
3 import { resolve } from 'node:path';
4 import { fileURLToPath } from 'node:url';
5 import { patchBundledGlobs } from './patch-bundled-globs.mjs';
6
7 export function classifyAudit(report) {
8 if (report.error || !report.vulnerabilities || !report.metadata?.vulnerabilities) throw new Error('Incomplete dependency audit');
9 const blocked = Object.keys(report.vulnerabilities);
10 if (blocked.length !== report.metadata.vulnerabilities.total) throw new Error('Dependency audit count mismatch');
11 return { upstreamFindings: report.metadata.vulnerabilities.total, blocked };
12 }
13
14 function main() {
15 const bundledProof = patchBundledGlobs({ checkOnly: true });
16 if (!process.env.npm_execpath) throw new Error('Run through npm run audit:dependencies');
17 const result = spawnSync(process.execPath, [process.env.npm_execpath, 'audit', '--json'], { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 });
18 if (result.error || result.signal || ![0, 1].includes(result.status)) throw new Error('Dependency audit failed to complete');
19 const report = JSON.parse(result.stdout);
20 const at = process.argv.indexOf('--report');
21 if (at >= 0) {
22 if (!process.argv[at + 1]) throw new Error('Missing audit report path');
23 writeFileSync(process.argv[at + 1], JSON.stringify(report, null, 2) + '\n');
24 }
25 const verdict = classifyAudit(report);
26 console.log(JSON.stringify({ ...verdict, bundledGlobMitigation: bundledProof }));
27 if (verdict.blocked.length) process.exitCode = 1;
28 }
29 if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main();
30
30 lines Plain Text