29 lines
851 B
TypeScript
29 lines
851 B
TypeScript
import { ChildProcess, SpawnSyncOptionsWithBufferEncoding, SpawnSyncReturns, spawnSync as rawSpawnSync } from "child_process";
|
|
|
|
export function check(result: SpawnSyncReturns<Buffer | string>) {
|
|
if (result.error)
|
|
throw result.error;
|
|
if (result.status !== 0) {
|
|
throw result.stderr;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function checkedSpawnSync(
|
|
command: string,
|
|
args: readonly string[],
|
|
options: SpawnSyncOptionsWithBufferEncoding,
|
|
): SpawnSyncReturns<Buffer | string> {
|
|
return check(rawSpawnSync(command, args, options));
|
|
}
|
|
|
|
export function promisifySpawn<T extends ChildProcess>(p: T) {
|
|
return new Promise<T>((resolve, reject) => {
|
|
p.on("error", err => reject(err));
|
|
p.on("close", number => {
|
|
if (number != 0) return reject(number);
|
|
resolve(p);
|
|
});
|
|
});
|
|
}
|