exec.ts (1018B)
1 import { execFile, type ExecFileOptions } from "node:child_process"; 2 3 export type ExecResult = { 4 status: number; 5 stdout: string; 6 stderr: string; 7 }; 8 9 /** 10 * Run a command without blocking the event loop. Unlike `execFile`'s promise 11 * form, this resolves for non-zero exit codes so callers can handle command 12 * failures explicitly. 13 */ 14 export function exec( 15 command: string, 16 args: readonly string[], 17 options: ExecFileOptions = {}, 18 ): Promise<ExecResult> { 19 return new Promise((resolve) => { 20 try { 21 execFile( 22 command, 23 args, 24 { maxBuffer: 10 * 1024 * 1024, ...options }, 25 (error, stdout, stderr) => { 26 const code = (error as NodeJS.ErrnoException | null)?.code; 27 resolve({ 28 status: typeof code === "number" ? code : error ? 1 : 0, 29 stdout: stdout?.toString() ?? "", 30 stderr: stderr?.toString() ?? "", 31 }); 32 }, 33 ); 34 } catch { 35 resolve({ status: 1, stdout: "", stderr: "" }); 36 } 37 }); 38 }