-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
child.js
149 lines (124 loc) · 3.78 KB
/
child.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* @flow */
/* global child_process$spawnOpts */
import * as constants from '../constants.js';
import BlockingQueue from './blocking-queue.js';
import {ProcessSpawnError, ProcessTermError} from '../errors.js';
import {promisify} from './promise.js';
const child = require('child_process');
export const queue = new BlockingQueue('child', constants.CHILD_CONCURRENCY);
// TODO: this uid check is kinda whack
let uid = 0;
export const exec = promisify(child.exec);
export function forkp(program: string, args: Array<string>, opts?: Object): Promise<number> {
return new Promise((resolve, reject) => {
const proc = child.fork(program, args, opts);
proc.on('error', error => {
reject(error);
});
proc.on('close', exitCode => {
resolve(exitCode);
});
});
}
export function spawnp(program: string, args: Array<string>, opts?: Object): Promise<number> {
return new Promise((resolve, reject) => {
const proc = child.spawn(program, args, opts);
proc.on('error', error => {
reject(error);
});
proc.on('close', exitCode => {
resolve(exitCode);
});
});
}
const spawnedProcesses = {};
export function forwardSignalToSpawnedProcesses(signal: string) {
for (const key of Object.keys(spawnedProcesses)) {
spawnedProcesses[key].kill(signal);
}
}
type ProcessFn = (
proc: child_process$ChildProcess,
update: (chunk: string) => void,
reject: (err: mixed) => void,
done: () => void,
) => void;
export function spawn(
program: string,
args: Array<string>,
opts?: child_process$spawnOpts & {detached?: boolean, process?: ProcessFn} = {},
onData?: (chunk: Buffer | string) => void,
): Promise<string> {
const key = opts.cwd || String(++uid);
return queue.push(
key,
(): Promise<string> =>
new Promise((resolve, reject) => {
const proc = child.spawn(program, args, opts);
spawnedProcesses[key] = proc;
let processingDone = false;
let processClosed = false;
let err = null;
let stdout = '';
proc.on('error', err => {
if (err.code === 'ENOENT') {
reject(new ProcessSpawnError(`Couldn't find the binary ${program}`, err.code, program));
} else {
reject(err);
}
});
function updateStdout(chunk: string) {
stdout += chunk;
if (onData) {
onData(chunk);
}
}
function finish() {
delete spawnedProcesses[key];
if (err) {
reject(err);
} else {
resolve(stdout.trim());
}
}
if (typeof opts.process === 'function') {
opts.process(proc, updateStdout, reject, function() {
if (processClosed) {
finish();
} else {
processingDone = true;
}
});
} else {
if (proc.stderr) {
proc.stderr.on('data', updateStdout);
}
if (proc.stdout) {
proc.stdout.on('data', updateStdout);
}
processingDone = true;
}
proc.on('close', (code: number, signal: string) => {
if (signal || code >= 1) {
err = new ProcessTermError(
[
'Command failed.',
signal ? `Exit signal: ${signal}` : `Exit code: ${code}`,
`Command: ${program}`,
`Arguments: ${args.join(' ')}`,
`Directory: ${opts.cwd || process.cwd()}`,
`Output:\n${stdout.trim()}`,
].join('\n'),
);
err.EXIT_SIGNAL = signal;
err.EXIT_CODE = code;
}
if (processingDone || err) {
finish();
} else {
processClosed = true;
}
});
}),
);
}