Skip to content

Commit

Permalink
The grunt.util.spawn method now supports a "grunt" option. Closes gh-526
Browse files Browse the repository at this point in the history
.
  • Loading branch information
cowboy committed Nov 15, 2012
1 parent 79eb03a commit a239982
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions docs/api_util.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ The `options` object has these possible properties:
var options = {
// The command to execute. It should be in the system path.
cmd: commandToExecute,
// If specified, the same grunt bin that is currently running will be
// spawned as the child command, instead of the "cmd" option. Defaults
// to false.
grunt: boolean,
// An array of arguments to pass to the command.
args: arrayOfArguments,
// Additional options for the Node.js child_process spawn method.
Expand Down
26 changes: 17 additions & 9 deletions lib/grunt/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,24 @@ util.spawn = function(opts, done) {
// On error (and no fallback) pass an error object, otherwise pass null.
done(code === 0 || 'fallback' in opts ? null : new Error(stderr), result, code);
};
// On Windows, child_process.spawn will only file .exe files in the PATH, not
// other executable types (grunt issue #155).
var cmd;
try {
cmd = which(opts.cmd);
} catch (err) {
callDone(127, '', String(err));
return;

var cmd, args;
if (opts.grunt) {
cmd = process.argv[0];
args = [process.argv[1]].concat(opts.args);
} else {
// On Windows, child_process.spawn will only file .exe files in the PATH,
// not other executable types (grunt issue #155).
try {
cmd = which(opts.cmd);
} catch (err) {
callDone(127, '', String(err));
return;
}
args = opts.args;
}
var child = spawn(cmd, opts.args, opts.opts);

var child = spawn(cmd, args, opts.opts);
var stdout = '';
var stderr = '';
child.stdout.on('data', function(buf) { stdout += buf; });
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/Gruntfile-print-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(grunt) {

grunt.registerTask('print', 'Print the specified text.', function(text) {
console.log('OUTPUT: ' + text);
// console.log(process.cwd());
});

};
25 changes: 25 additions & 0 deletions test/grunt/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ exports['util.spawn'] = {
test.done();
});
},
'grunt': function(test) {
test.expect(3);
grunt.util.spawn({
grunt: true,
args: [ '--gruntfile', 'test/fixtures/Gruntfile-print-text.js', 'print:foo' ],
}, function(err, result, code) {
test.equals(err, null);
test.equals(code, 0);
test.ok(/^OUTPUT: foo/m.test(result.stdout), 'stdout should contain output indicating the grunt task was run.');
test.done();
});
},
'grunt (with cwd)': function(test) {
test.expect(3);
grunt.util.spawn({
grunt: true,
args: [ '--gruntfile', 'Gruntfile-print-text.js', 'print:foo' ],
opts: {cwd: 'test/fixtures'},
}, function(err, result, code) {
test.equals(err, null);
test.equals(code, 0);
test.ok(/^OUTPUT: foo/m.test(result.stdout), 'stdout should contain output indicating the grunt task was run.');
test.done();
});
},
};

exports['util.underscore.string'] = function(test) {
Expand Down

0 comments on commit a239982

Please sign in to comment.