|
1 | | -/** |
2 | | - * Pastoralist override audit for the optimize command. Runs pastoralist's |
3 | | - * update pipeline against the project so stale package-manager overrides get |
4 | | - * the review record (and pruning) pastoralist maintains, before socket's own. |
5 | | - * |
6 | | - * @socketregistry overrides are applied. |
7 | | - * |
8 | | - * Key Functions: - runPastoralistAudit: spawn the pinned pastoralist bin |
9 | | - * against the project root, never failing the optimize run on its errors. |
10 | | - * |
11 | | - * Spawned (not imported) for the same reason the package-manager agents are: |
12 | | - * the dependency stays out of the CLI's CJS bundle (pastoralist's dist uses |
13 | | - * top-level await, which rolldown cannot emit as CJS) while the lockfile |
14 | | - * still pins the exact audited version. |
15 | | - */ |
16 | | - |
17 | | -import { fileURLToPath } from 'node:url' |
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import { findPackageJSON } from 'node:module' |
| 3 | +import path from 'node:path' |
| 4 | +import { pathToFileURL } from 'node:url' |
18 | 5 |
|
19 | 6 | import { debug, debugDir } from '@socketsecurity/lib-stable/debug/output' |
20 | 7 | import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default' |
21 | 8 | import { spawn } from '@socketsecurity/lib-stable/process/spawn/child' |
| 9 | +import { rootPath } from '../../constants/paths.mts' |
| 10 | +import { resolveNodeRuntime } from '../../util/spawn/node-runtime.mts' |
| 11 | + |
22 | 12 | const logger = getDefaultLogger() |
23 | 13 |
|
24 | 14 | export type PastoralistAuditResult = { |
25 | 15 | ok: boolean |
26 | 16 | reason?: string | undefined |
27 | 17 | } |
28 | 18 |
|
29 | | -/** |
30 | | - * Run pastoralist's override audit against `root`: its update flow writes |
31 | | - * the override review appendix and prunes overrides whose reason is gone. |
32 | | - * Errors are logged and swallowed — an audit miss must never block the |
33 | | - * optimization it precedes. |
34 | | - */ |
35 | 19 | export async function runPastoralistAudit( |
36 | 20 | root: string, |
37 | 21 | ): Promise<PastoralistAuditResult> { |
38 | 22 | let binPath: string |
39 | 23 | try { |
40 | | - // pastoralist's exports map carries only an `import` condition for `.` |
41 | | - // (dist/index.js, the bin), so the import-condition resolver finds it |
42 | | - // where a require-resolve cannot. |
43 | | - binPath = fileURLToPath(import.meta.resolve('pastoralist')) |
| 24 | + const manifest = findPackageJSON( |
| 25 | + 'pastoralist', |
| 26 | + pathToFileURL(path.join(rootPath, 'package.json')), |
| 27 | + ) |
| 28 | + if (!manifest) { |
| 29 | + return { ok: false, reason: 'pastoralist is not installed' } |
| 30 | + } |
| 31 | + const packageJson = JSON.parse(readFileSync(manifest, 'utf8')) as { |
| 32 | + bin: { pastoralist: string } |
| 33 | + } |
| 34 | + binPath = path.resolve(path.dirname(manifest), packageJson.bin.pastoralist) |
44 | 35 | } catch (e) { |
45 | 36 | debug('pastoralist is not resolvable from this checkout') |
46 | 37 | debugDir(e) |
47 | 38 | return { ok: false, reason: 'pastoralist is not installed' } |
48 | 39 | } |
49 | 40 |
|
50 | | - const result = await spawn(process.execPath, [binPath, '--root', root], { |
| 41 | + const runtime = await resolveNodeRuntime({ cwd: root }) |
| 42 | + const result = await spawn(runtime.executable, [binPath, '--root', root], { |
| 43 | + env: runtime.environment, |
51 | 44 | cwd: root, |
52 | 45 | stdio: 'inherit', |
53 | 46 | }) |
|
0 commit comments