Skip to content

Commit

Permalink
Removed repeating function and replaced it by template generator
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Sep 28, 2010
1 parent 347dcaa commit 9243dee
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down

0 comments on commit 9243dee

Please sign in to comment.