-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
execAsync.ts
50 lines (44 loc) · 1.42 KB
/
execAsync.ts
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
import {exec} from 'child_process';
import type {ChildProcess} from 'child_process';
import * as Logger from './logger';
type PromiseWithAbort = Promise<string | void> & {
abort?: () => void;
};
/**
* Executes a command none-blocking by wrapping it in a promise.
* In addition to the promise it returns an abort function.
*/
export default (command: string, env: NodeJS.ProcessEnv = {}): PromiseWithAbort => {
let childProcess: ChildProcess;
const promise: PromiseWithAbort = new Promise<string | void>((resolve, reject) => {
const finalEnv: NodeJS.ProcessEnv = {
...process.env,
...env,
};
Logger.note(command);
childProcess = exec(
command,
{
maxBuffer: 1024 * 1024 * 10, // Increase max buffer to 10MB, to avoid errors
env: finalEnv,
},
(error, stdout) => {
if (error) {
if (error && error.killed) {
resolve();
} else {
Logger.error(`failed with error: ${error.message}`);
reject(error);
}
} else {
Logger.writeToLogFile(stdout);
resolve(stdout);
}
},
);
});
promise.abort = () => {
childProcess.kill('SIGINT');
};
return promise;
};