| 1 | const fs = require('fs'); |
| 2 | |
| 3 | function patchStream(stream, fd) { |
| 4 | const originalWrite = stream.write.bind(stream); |
| 5 | |
| 6 | stream.write = function write(chunk, encoding, callback) { |
| 7 | try { |
| 8 | if (typeof encoding === 'function') { |
| 9 | callback = encoding; |
| 10 | encoding = undefined; |
| 11 | } |
| 12 | |
| 13 | const buffer = Buffer.isBuffer(chunk) |
| 14 | ? chunk |
| 15 | : Buffer.from(String(chunk), encoding || 'utf8'); |
| 16 | |
| 17 | fs.writeSync(fd, buffer); |
| 18 | if (typeof callback === 'function') callback(); |
| 19 | return true; |
| 20 | } catch (error) { |
| 21 | return originalWrite(chunk, encoding, callback); |
| 22 | } |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | patchStream(process.stdout, 1); |
| 27 | patchStream(process.stderr, 2); |