import { spawn } from 'node:child_process'; import { mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; const DEFAULT_COMMAND_TIMEOUT_MS = 30_000; export function resolveBinaryPath(configured: string | undefined, envKeys: string[], label: string): string { const fromEnv = envKeys.map((key) => process.env[key]).find(Boolean); const binaryPath = configured ?? fromEnv; if (!binaryPath) { throw new Error( `${label} path is required. Provide it via the provider config or ${envKeys.join(' / ')}.`, ); } return binaryPath; } export async function runBinary(binary: string, args: string[], timeoutMs = DEFAULT_COMMAND_TIMEOUT_MS): Promise { await new Promise((resolve, reject) => { const child = spawn(binary, args, { stdio: ['ignore', 'pipe', 'pipe'], }); let settled = false; let stderr = ''; const timer = setTimeout(() => { if (settled) { return; } settled = true; child.kill('SIGKILL'); reject(new Error(`${binary} timed out after ${timeoutMs}ms`)); }, timeoutMs); child.stderr.on('data', (chunk) => { stderr += chunk.toString(); }); child.on('error', (error) => { if (settled) { return; } settled = true; clearTimeout(timer); reject(error); }); child.on('close', (code) => { if (settled) { return; } settled = true; clearTimeout(timer); if (code === 0) { resolve(); return; } reject(new Error(stderr.trim() || `${binary} exited with code ${code ?? 'unknown'}`)); }); }); } export async function runBinaryCapture( binary: string, args: string[], timeoutMs = DEFAULT_COMMAND_TIMEOUT_MS, ): Promise { return await new Promise((resolve, reject) => { const child = spawn(binary, args, { stdio: ['ignore', 'pipe', 'pipe'], }); let settled = false; let stdout = ''; let stderr = ''; const timer = setTimeout(() => { if (settled) { return; } settled = true; child.kill('SIGKILL'); reject(new Error(`${binary} timed out after ${timeoutMs}ms`)); }, timeoutMs); child.stdout.on('data', (chunk) => { stdout += chunk.toString(); }); child.stderr.on('data', (chunk) => { stderr += chunk.toString(); }); child.on('error', (error) => { if (settled) { return; } settled = true; clearTimeout(timer); reject(error); }); child.on('close', (code) => { if (settled) { return; } settled = true; clearTimeout(timer); if (code === 0) { resolve(stdout); return; } reject(new Error(stderr.trim() || `${binary} exited with code ${code ?? 'unknown'}`)); }); }); } export async function withTempDir(prefix: string, runner: (dir: string) => Promise): Promise { const dir = await mkdtemp(join(tmpdir(), prefix)); try { return await runner(dir); } finally { await rm(dir, { recursive: true, force: true }); } }