• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Hijack stdout and stderr
4const stdWrite = {};
5function hijackStdWritable(name, listener) {
6  const stream = process[name];
7  const _write = stdWrite[name] = stream.write;
8
9  stream.writeTimes = 0;
10  stream.write = function(data, callback) {
11    try {
12      listener(data);
13    } catch (e) {
14      process.nextTick(() => { throw e; });
15    }
16
17    _write.call(stream, data, callback);
18    stream.writeTimes++;
19  };
20}
21
22function restoreWritable(name) {
23  process[name].write = stdWrite[name];
24  delete process[name].writeTimes;
25}
26
27module.exports = {
28  hijackStdout: hijackStdWritable.bind(null, 'stdout'),
29  hijackStderr: hijackStdWritable.bind(null, 'stderr'),
30  restoreStdout: restoreWritable.bind(null, 'stdout'),
31  restoreStderr: restoreWritable.bind(null, 'stderr')
32};
33