Use process.argv to spawn grunt

Will use the node and grunt bin that originally spawned
the process to spawn child processes.
This commit is contained in:
Kyle Robinson Young 2012-10-24 10:25:37 -07:00
parent faaa0d1118
commit 8f4bd99f40
2 changed files with 15 additions and 30 deletions

View File

@ -25,18 +25,6 @@ module.exports = function(grunt) {
interrupt: false
};
// Find the grunt bin
var gruntBin = grunt.util._.find([
path.resolve(process.cwd(), 'node_modules', '.bin', 'grunt'),
process.argv[1]
], function(bin) {
return fs.existsSync(bin);
});
if (process.platform === 'win32') { gruntBin += '.cmd'; }
if (!fs.existsSync(gruntBin)) {
grunt.fatal('The Grunt binary could not be found. Please install grunt first with: npm install grunt');
}
grunt.registerTask('watch', 'Run predefined tasks whenever watched files change.', function(target) {
this.requiresConfig('watch');
// Build an array of files/tasks objects
@ -92,9 +80,12 @@ module.exports = function(grunt) {
changedFiles = Object.create(null);
// Spawn the tasks as a child process
spawned[i] = grunt.util.spawn({
cmd: gruntBin,
// Use the node that spawned this process
cmd: process.argv[0],
// Run from current working dir
opts: {cwd: process.cwd()},
args: grunt.util._.union(tasks, [].slice.call(process.argv, 3))
// Run grunt this process uses, append the task to be run and any cli options
args: grunt.util._.union([process.argv[1]].concat(tasks), [].slice.call(process.argv, 2))
}, function(err, res, code) {
// Spawn is done
delete spawned[i];

View File

@ -4,6 +4,9 @@ var grunt = require('grunt');
var path = require('path');
grunt.util = grunt.util || grunt.utils;
// Node v0.6 compat
path.sep = path.sep || path.normalize('/');
// In case the grunt being used to test is different than the grunt being
// tested, initialize the task and config subsystems.
if (grunt.task.searchDirs.length === 0) {
@ -26,18 +29,6 @@ function assertTask(task, options) {
task = task || 'default';
options = options || {};
// Find the grunt bin
var gruntBin = grunt.util._.find([
path.resolve(process.cwd(), 'node_modules', '.bin', 'grunt'),
process.argv[1]
], function(bin) {
return grunt.file.exists(bin);
});
if (process.platform === 'win32') { gruntBin += '.cmd'; }
if (!grunt.file.exists(gruntBin)) {
grunt.fatal('The Grunt binary could not be found.');
}
// get next/kill process trigger
var trigger = options.trigger || 'Waiting...';
delete options.trigger;
@ -46,17 +37,20 @@ function assertTask(task, options) {
var cwd = options.cwd || process.cwd();
delete options.cwd;
// turn options into spawn options
var spawnOptions = [];
// Use grunt this process uses
var spawnOptions = [process.argv[1]];
// Turn options into spawn options
grunt.util._.each(options, function(val, key) {
spawnOptions.push('--' + key);
spawnOptions.push(val);
});
// Add the tasks to run
spawnOptions.push(task);
// Return an interface for testing this task
function returnFunc(runs, done) {
var spawnGrunt = spawn(gruntBin, spawnOptions, {cwd:cwd});
// Spawn the node this process uses
var spawnGrunt = spawn(process.argv[0], spawnOptions, {cwd:cwd});
var out = '';
if (!grunt.util._.isArray(runs)) {
@ -112,7 +106,7 @@ exports.watchConfig = {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
}, function(result) {
verboseLog(result);
test.ok(result.indexOf('File "lib/one.js" changed') !== -1, 'Watch should have fired when oneTarget/lib/one.js has changed.');
test.ok(result.indexOf('File "lib' + path.sep + 'one.js" changed') !== -1, 'Watch should have fired when oneTarget/lib/one.js has changed.');
test.ok(result.indexOf('I do absolutely nothing.') !== -1, 'echo task should have fired.');
test.done();
});