Spaces:
Running
Running
File size: 1,733 Bytes
ac68ae4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import { execa, type ExecaError, type Options } from "execa";
export type RunResult = {
exitCode: number;
stdout: string;
stderr: string;
ok: boolean;
};
export async function runQuiet(
bin: string,
args: string[] = [],
options: Options = {},
): Promise<RunResult> {
try {
const result = await execa(bin, args, {
reject: false,
stdout: "pipe",
stderr: "pipe",
...options,
});
return {
exitCode: result.exitCode ?? 1,
stdout: result.stdout?.toString() ?? "",
stderr: result.stderr?.toString() ?? "",
ok: (result.exitCode ?? 1) === 0,
};
} catch (err) {
const ee = err as ExecaError;
return {
exitCode: typeof ee.exitCode === "number" ? ee.exitCode : 1,
stdout: ee.stdout?.toString() ?? "",
stderr: ee.stderr?.toString() ?? (ee.message ?? ""),
ok: false,
};
}
}
export async function runInherit(
bin: string,
args: string[] = [],
options: Options = {},
): Promise<RunResult> {
try {
const result = await execa(bin, args, {
reject: false,
stdout: "inherit",
stderr: "inherit",
...options,
});
return {
exitCode: result.exitCode ?? 1,
stdout: "",
stderr: "",
ok: (result.exitCode ?? 1) === 0,
};
} catch (err) {
const ee = err as ExecaError;
return {
exitCode: typeof ee.exitCode === "number" ? ee.exitCode : 1,
stdout: "",
stderr: ee.message ?? "",
ok: false,
};
}
}
export async function which(bin: string): Promise<boolean> {
const finder = process.platform === "win32" ? "where" : "which";
const result = await runQuiet(finder, [bin]);
return result.ok && result.stdout.trim().length > 0;
}
|