-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind-std-to-console.js
141 lines (112 loc) · 4.29 KB
/
bind-std-to-console.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
const { Writable } = require('stream');
const consoleTypeMessage = Symbol('console');
function BindStdToConsole() {
this._stdoutIncompleteLine = '';
this._stdoutChunkFromConsole = undefined;
this._stdoutWriteToOrigin = process.stdout.write.bind(process.stdout);
this._stdoutRaw = undefined;
this._stdoutWrite = undefined;
this._stderrIncompleteLine = '';
this._stderrChunkFromConsole = undefined;
this._stderrWriteToOrigin = process.stderr.write.bind(process.stderr);
this._stderrRaw = undefined;
this._stderrWrite = undefined;
this._started = false;
// Method used to pipe the stdout
this.stdoutToConsole = console.log;
// method used to pipe the stderr
this.stderrToConsole = console.error;
if (process.env.NODE_ENV === 'development') {
this.start();
}
}
BindStdToConsole.prototype.start = function () {
const self = this;
// Avoid to be executed twice
if (this._started) {
return;
}
this._started = true;
this._stdoutRaw = process.stdout.write;
this._stderrRaw = process.stderr.write;
const processStdout = new Writable({
write(chunk, encoding, callback) {
const chunkString = `${chunk}`;
if (chunk[consoleTypeMessage]) {
if (self._stdoutChunkFromConsole) {
self._stdoutChunkFromConsole = undefined;
return callback();
}
self._stdoutWriteToOrigin(chunkString);
return callback();
}
self._stdoutIncompleteLine += chunkString;
const breakLine = self._stdoutIncompleteLine.lastIndexOf('\n');
if (breakLine > -1) {
const log = self._stdoutIncompleteLine.slice(0, breakLine);
self._stdoutIncompleteLine = self._stdoutIncompleteLine.slice(breakLine + 1) || '';
self._stdoutChunkFromConsole = chunk;
self.stdoutToConsole.call(console, log);
}
self._stdoutWriteToOrigin(chunk);
callback();
}
});
const consoleStdout = new Writable({
write(chunk, encoding, callback) {
const buf = Buffer.from(chunk);
buf[consoleTypeMessage] = true;
processStdout.write(buf);
callback();
}
});
this._stdoutWrite = processStdout.write.bind(processStdout);
process.stdout.write = this._stdoutWrite;
console._stdout = consoleStdout;
const processStderr = new Writable({
write(chunk, encoding, callback) {
const chunkString = `${chunk}`;
if (chunk[consoleTypeMessage]) {
if (self._stderrChunkFromConsole) {
self._stderrChunkFromConsole = undefined;
return callback();
}
self._stderrWriteToOrigin(chunkString);
return callback();
}
self._stderrIncompleteLine += chunkString;
const breakLine = self._stderrIncompleteLine.lastIndexOf('\n');
if (breakLine > -1) {
const log = self._stderrIncompleteLine.slice(0, breakLine);
self._stderrIncompleteLine = self._stderrIncompleteLine.slice(breakLine + 1) || '';
self._stderrChunkFromConsole = chunk;
self.stderrToConsole.call(console, log);
}
self._stderrWriteToOrigin(chunk);
callback();
}
});
const consoleStderr = new Writable({
write(chunk, encoding, callback) {
const buf = Buffer.from(chunk);
buf[consoleTypeMessage] = true;
processStderr.write(buf);
callback();
}
});
this._stderrWrite = processStderr.write.bind(processStderr);
process.stderr.write = this._stderrWrite;
console._stderr = consoleStderr;
};
BindStdToConsole.prototype.stop = function () {
this._started = false;
if (this._stdoutWrite === process.stdout.write && this._stdoutRaw) {
process.stdout.write = this._stdoutRaw;
console._stdout = process.stdout;
}
if (this._stderrWrite === process.stderr.write && this._stderrRaw) {
process.stderr.write = this._stderrRaw;
console._stderr = process.stderr;
}
};
module.exports = new BindStdToConsole();