-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (39 loc) · 1.07 KB
/
index.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
/* jshint node: true */
var originalStdout = process.stdout.write;
var originalStderr = process.stderr.write;
var outData = [];
var errData = [];
function collect(arr) {
return function (val) {
/* istanbul ignore next */
try {
// Buffer.from was officially introduced
// in node 4.5.0, but was actually present
// with only partial abilities before that.
// In earlier versions of node 4, it could
// not handle string data. Simply testing
// for the existence of Buffer.from does not
// support all versions of node.
arr.push(Buffer.from(val));
} catch(e) {
arr.push(new Buffer(val));
}
};
}
module.exports = {
start: function () {
process.stdout.write = collect(outData);
process.stderr.write = collect(errData);
},
end: function () {
process.stdout.write = originalStdout;
process.stderr.write = originalStderr;
var data = {
stdout: Buffer.concat(outData).toString(),
stderr: Buffer.concat(errData).toString()
};
outData = [];
errData = [];
return data;
}
};