-
Notifications
You must be signed in to change notification settings - Fork 18
/
StdIo.js
74 lines (61 loc) · 1.68 KB
/
StdIo.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
class StdIo {
constructor() {
this._out = process.stdout.write.bind(process.stdout);
this._err = process.stderr.write.bind(process.stderr);
this._bufferedOutput = new Set();
this._wrapStdio(process.stdout);
this._wrapStdio(process.stderr);
}
log(message) {
process.stderr.write(message + '\n');
}
logInline(message) {
process.stderr.write(message);
}
close() {
this._forceFlushBufferedOutput();
process.stdout.write = this._out;
process.stderr.write = this._err;
}
// Don't wait for the debounced call and flush all output immediately.
_forceFlushBufferedOutput() {
for (const flushBufferedOutput of this._bufferedOutput) {
flushBufferedOutput();
}
}
_wrapStdio(stream) {
const originalWrite = stream.write;
let buffer = [];
let timeout = null;
const flushBufferedOutput = () => {
const string = buffer.join('');
buffer = [];
if (string) {
originalWrite.call(stream, string);
}
this._bufferedOutput.delete(flushBufferedOutput);
};
this._bufferedOutput.add(flushBufferedOutput);
const debouncedFlush = () => {
// If the process blows up no errors would be printed.
// There should be a smart way to buffer stderr, but for now
// we just won't buffer it.
if (stream === process.stderr) {
flushBufferedOutput();
} else {
if (!timeout) {
timeout = setTimeout(() => {
flushBufferedOutput();
timeout = null;
}, 100);
}
}
};
stream.write = chunk => {
buffer.push(chunk);
debouncedFlush();
return true;
};
}
}
module.exports = StdIo;