import { execFile } from "node:child_process";
import { promisify } from "node:util";

const execFileAsync = promisify(execFile);

export interface ExecFileResult {
  stdout: string;
  stderr: string;
  status: 0 | 1;
}

/**
 * Run an external binary and return its output without throwing.
 * Returns status 0 on success, status 1 on error (non-zero exit or spawn failure).
 */
export async function execFileNoThrow(
  file: string,
  args: string[],
): Promise<ExecFileResult> {
  try {
    const result = await execFileAsync(file, args, { timeout: 10_000 });
    return { stdout: result.stdout, stderr: result.stderr, status: 0 };
  } catch {
    return { stdout: "", stderr: "", status: 1 };
  }
}
