diff --git a/lib/forever.js b/lib/forever.js index 63c63de2..1201d0ba 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -42,33 +42,29 @@ Forever.prototype.run = function () { var self = this, child = spawn('node', this.options.options); this.child = child; - child.stdout.on('data', function (data) { - // If we haven't been silenced, write to the process stdout stream - if (!self.options.silent) { - process.stdout.write(data); - } - - // If we have been given an output file for stdout, write to it - if (self.options.stdout) { - self.stdout.write(data); - } + // Hook all stream data + // And process it + function hookStream(streamType) { + child[streamType].on('data', function (data) { + // If we haven't been silenced, write to the process stdout stream + if (!self.options.silent) { + process[streamType].write(data); + } + + // If we have been given an output file for stdout, write to it + if (self.options[streamType]) { + self[streamType].write(data); + } + + self.emit(streamType, null, data); + }); - self.emit('stdout', null, data); - }); + // Chaining + return hookStream; + } - child.stderr.on('data', function (data) { - // If we haven't been silenced, write to the process stdout stream - if (!self.options.silent) { - process.stdout.write(data); - } - - // If we have been given an output file for stderr, write to it - if (self.options.stderr) { - self.stderr.write(data); - } - - self.emit('stderr', null, data); - }); + // Hook stdout and stderr + hookStream('stdout')('stderr'); child.on('exit', function (code) { self.times++;