sin-github-issues / src /command.ts
delqhi's picture
Bootstrap SIN-GitHub-Issues runtime
a4dc18c verified
Raw
History Blame Contribute Delete
895 Bytes
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
export async function runCommand(command: string, args: string[], cwd?: string) {
try {
const result = await execFileAsync(command, args, {
cwd,
maxBuffer: 1024 * 1024 * 8,
env: process.env,
});
return { stdout: result.stdout.trim(), stderr: result.stderr.trim() };
} catch (error) {
const failure = error as { stdout?: string; stderr?: string; message?: string };
const details = (failure.stderr || failure.stdout || failure.message || 'command_failed').trim();
throw new Error(`${command}_failed:${details}`);
}
}
export async function runJsonCommand<T>(command: string, args: string[], cwd?: string): Promise<T> {
const result = await runCommand(command, args, cwd);
return JSON.parse(result.stdout) as T;
}