Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waiting for output streams to have finished before exiting grunt. #642

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ grunt.tasks = function(tasks, options, done) {
done();
} else {
// Otherwise, explicitly exit.
process.exit(0);
var exitProcess = require('./util/exit');
exitProcess(0);
}
}
});
Expand Down
7 changes: 4 additions & 3 deletions lib/grunt/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var grunt = require('../grunt');

// The module to be exported.
var fail = module.exports = {};
var exitProcess = require('../util/exit');

// Error codes
// 1. Generic error.
Expand All @@ -36,10 +37,10 @@ function writeln(e, mode) {
grunt.log.writeln([tags[mode][0], msg.yellow, tags[mode][1]].join(' '));
}

// A fatal error occured. Abort immediately.
// A fatal error occured. Abort immediately (as soon as stdout and stderr have received all their data).
fail.fatal = function(e, errcode) {
writeln(e, 'fatal');
process.exit(typeof errcode === 'number' ? errcode : 1);
exitProcess(typeof errcode === 'number' ? errcode : 1);
};

// Keep track of error and warning counts.
Expand Down Expand Up @@ -70,7 +71,7 @@ fail.warn = function(e, errcode) {
}
// Log and exit.
grunt.log.writeln().fail('Aborted due to warnings.');
process.exit(typeof errcode === 'number' ? errcode : 2);
exitProcess(typeof errcode === 'number' ? errcode : 2);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/grunt/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ grunt.log.writeln().writelns(
'\n\n' +
'For more information, see http://gruntjs.com/'
);

process.exit();
var exitProcess = require('../util/exit');
exitProcess();
25 changes: 25 additions & 0 deletions lib/util/exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Waits for the stream to have fully received its content.
var waitForStreamEnd = function (stream, callback) {
// Writes an empty string to check the state of the buffer:
if (!stream.write("")) {
stream.once('drain', callback);
} else {
callback();
}
};

// Exits the process asynchronously, making sure the stdout and stderr streams have fully received their content.
var exitProcess = function (exitCode) {
var ok = 0;
var increaseOk = function () {
ok++;
if (ok == 3) {
process.exit(exitCode);
}
};
waitForStreamEnd(process.stdout, increaseOk);
waitForStreamEnd(process.stderr, increaseOk);
process.nextTick(increaseOk);
};

module.exports = exitProcess;