Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created October 2, 2012 13:31
Show Gist options
  • Save cowboy/3819170 to your computer and use it in GitHub Desktop.
Save cowboy/3819170 to your computer and use it in GitHub Desktop.
Grunt 0.4.0a (devel) Run a subproject's grunt tasks.
/*
* 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);
});
});
};
// Project configuration.
grunt.initConfig({
subgrunt: {
foo: {
subdir: './foo/',
args: ['jshint', 'qunit'],
},
},
});
@cowboy
Copy link
Author

cowboy commented Oct 2, 2012

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

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

@cowboy
Copy link
Author

cowboy commented Oct 2, 2012

(Note: untested in Windows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment