public
Last active

Grunt 0.4.0a (devel) Run a subproject's grunt tasks.

  • Download Gist
grunt-subgrunt.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
* grunt-subgrunt
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
 
'use strict';
 
module.exports = function(grunt) {
 
// Nodejs libs.
var path = require('path');
 
grunt.registerMultiTask('subgrunt', 'Run a sub-project\'s grunt tasks.', function() {
if (!grunt.file.exists(this.data.subdir)) {
grunt.log.error('Directory "' + this.data.subdir + '" not found.');
return false;
}
var done = this.async();
var subdir = path.resolve(this.data.subdir);
grunt.util.spawn({
cmd: path.join(subdir, 'node_modules/.bin/grunt'),
args: this.data.args || [],
opts: {cwd: subdir},
}, function(error, result, code) {
if (code === 127) {
grunt.log.error('Error running sub-grunt. Did you run "npm install" in the sub-project?');
} else {
grunt.log.writeln('\n' + result.stdout);
}
done(code === 0);
});
});
 
};
usage.js
JavaScript
1 2 3 4 5 6 7 8 9
// Project configuration.
grunt.initConfig({
subgrunt: {
foo: {
subdir: './foo/',
args: ['jshint', 'qunit'],
},
},
});

In this example, grunt subgrunt:foo would run grunt in the foo subdirectory.

Assuming that npm install had been run in there first :)

(Note: untested in Windows)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.