| 65 | } |
| 66 | |
| 67 | protected __wrapStdio(stream: NodeJS.WritableStream | WriteStream): void { |
| 68 | const write = stream.write.bind(stream); |
| 69 | |
| 70 | let buffer: Array<string> = []; |
| 71 | let timeout: NodeJS.Timeout | null = null; |
| 72 | |
| 73 | const flushBufferedOutput = () => { |
| 74 | const string = buffer.join(''); |
| 75 | buffer = []; |
| 76 | |
| 77 | // This is to avoid conflicts between random output and status text |
| 78 | this.__beginSynchronizedUpdate( |
| 79 | this._globalConfig.useStderr ? this._err : this._out, |
| 80 | ); |
| 81 | this.__clearStatus(); |
| 82 | if (string) { |
| 83 | write(string); |
| 84 | } |
| 85 | this.__printStatus(); |
| 86 | this.__endSynchronizedUpdate( |
| 87 | this._globalConfig.useStderr ? this._err : this._out, |
| 88 | ); |
| 89 | |
| 90 | this._bufferedOutput.delete(flushBufferedOutput); |
| 91 | }; |
| 92 | |
| 93 | this._bufferedOutput.add(flushBufferedOutput); |
| 94 | |
| 95 | const debouncedFlush = () => { |
| 96 | // If the process blows up no errors would be printed. |
| 97 | // There should be a smart way to buffer stderr, but for now |
| 98 | // we just won't buffer it. |
| 99 | if (stream === process.stderr) { |
| 100 | flushBufferedOutput(); |
| 101 | } else { |
| 102 | if (!timeout) { |
| 103 | timeout = setTimeout(() => { |
| 104 | flushBufferedOutput(); |
| 105 | timeout = null; |
| 106 | }, 100); |
| 107 | } |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | stream.write = (chunk: string) => { |
| 112 | buffer.push(chunk); |
| 113 | debouncedFlush(); |
| 114 | return true; |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | // Don't wait for the debounced call and flush all output immediately. |
| 119 | forceFlushBufferedOutput(): void { |