@@ -7,6 +7,8 @@ import {
|
7 | 7 | } from "./deadcode-unused-files.allowlist.mjs"; |
8 | 8 | |
9 | 9 | const KNIP_VERSION = "6.8.0"; |
| 10 | +export const KNIP_TIMEOUT_MS = 10 * 60 * 1000; |
| 11 | +export const KNIP_MAX_BUFFER_BYTES = 16 * 1024 * 1024; |
10 | 12 | const KNIP_ARGS = [ |
11 | 13 | "--config", |
12 | 14 | "config/knip.config.ts", |
@@ -108,18 +110,28 @@ export function formatUnusedFileComparison(comparison) {
|
108 | 110 | return lines.join("\n"); |
109 | 111 | } |
110 | 112 | |
111 | | -export function runKnipUnusedFiles() { |
112 | | -const result = spawnSync( |
| 113 | +function spawnErrorCode(error) { |
| 114 | +return error && typeof error === "object" && "code" in error ? String(error.code) : undefined; |
| 115 | +} |
| 116 | + |
| 117 | +export function runKnipUnusedFiles(params = {}) { |
| 118 | +const run = params.spawnSyncCommand ?? spawnSync; |
| 119 | +const result = run( |
113 | 120 | "pnpm", |
114 | 121 | ["--config.minimum-release-age=0", "dlx", `knip@${KNIP_VERSION}`, ...KNIP_ARGS], |
115 | 122 | { |
116 | 123 | encoding: "utf8", |
| 124 | +killSignal: "SIGTERM", |
| 125 | +maxBuffer: params.maxBufferBytes ?? KNIP_MAX_BUFFER_BYTES, |
117 | 126 | stdio: ["ignore", "pipe", "pipe"], |
| 127 | +timeout: params.timeoutMs ?? KNIP_TIMEOUT_MS, |
118 | 128 | }, |
119 | 129 | ); |
120 | 130 | return { |
121 | 131 | status: result.status, |
122 | 132 | signal: result.signal, |
| 133 | +errorCode: spawnErrorCode(result.error), |
| 134 | +errorMessage: result.error?.message, |
123 | 135 | output: `${result.stdout ?? ""}${result.stderr ?? ""}`, |
124 | 136 | }; |
125 | 137 | } |
@@ -144,6 +156,18 @@ export function checkUnusedFiles(
|
144 | 156 | |
145 | 157 | function main() { |
146 | 158 | const result = runKnipUnusedFiles(); |
| 159 | +if (result.errorCode || result.status === null) { |
| 160 | +console.error( |
| 161 | +`deadcode unused-file scan failed: ${result.errorCode ?? result.signal ?? "unknown"}${ |
| 162 | + result.errorMessage ? `: ${result.errorMessage}` : "" |
| 163 | + }`, |
| 164 | +); |
| 165 | +if (result.output) { |
| 166 | +console.error(result.output); |
| 167 | +} |
| 168 | +process.exitCode = 1; |
| 169 | +return; |
| 170 | +} |
147 | 171 | const check = checkUnusedFiles(result.output); |
148 | 172 | if (!check.ok) { |
149 | 173 | if (check.message) { |
|