cli: add promisify spawn
This commit is contained in:
12
cli.ts
12
cli.ts
@@ -1,4 +1,4 @@
|
||||
import { SpawnSyncOptionsWithBufferEncoding, SpawnSyncReturns, spawnSync as rawSpawnSync } from "child_process";
|
||||
import { ChildProcess, SpawnSyncOptionsWithBufferEncoding, SpawnSyncReturns, spawnSync as rawSpawnSync } from "child_process";
|
||||
|
||||
export function check(result: SpawnSyncReturns<Buffer | string>) {
|
||||
if (result.error)
|
||||
@@ -16,3 +16,13 @@ export function checkedSpawnSync(
|
||||
): 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user