mirror of
https://github.com/MadsLorentzen/ai-job-search.git
synced 2026-09-17 08:36:25 +00:00
40 lines
959 B
TypeScript
40 lines
959 B
TypeScript
import { join } from "path";
|
|||
|
|
|
||
|
|
const CLI_PATH = join(import.meta.dir, "../src/cli.ts");
|
||
|
|
|
||
|
|
export interface CLIResult {
|
||
|
|
stdout: string;
|
||
|
|
stderr: string;
|
||
|
|
exitCode: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function runCLI(args: string[]): Promise<CLIResult> {
|
||
|
|
const proc = Bun.spawn(["bun", "run", CLI_PATH, ...args], {
|
||
|
|
stdout: "pipe",
|
||
|
|
stderr: "pipe",
|
||
|
|
});
|
||
|
|
|
||
|
|
const [stdout, stderr, exitCode] = await Promise.all([
|
||
|
|
new Response(proc.stdout).text(),
|
||
|
|
new Response(proc.stderr).text(),
|
||
|
|
proc.exited,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
|
||
|
|
}
|
||
|
|
|
||
|
|
export function parseJSON<T = unknown>(result: CLIResult): T {
|
||
|
|
if (result.exitCode !== 0) {
|
||
|
|
throw new Error(
|
||
|
|
`CLI exited with code ${result.exitCode}. stderr: ${result.stderr}`
|
||
|
|
);
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return JSON.parse(result.stdout) as T;
|
||
|
|
} catch {
|
||
|
|
throw new Error(
|
||
|
|
`Failed to parse JSON. stdout: ${result.stdout}\nstderr: ${result.stderr}`
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|