Skip to content

Instantly share code, notes, and snippets.

@drkibitz
Last active December 24, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drkibitz/6732076 to your computer and use it in GitHub Desktop.
Save drkibitz/6732076 to your computer and use it in GitHub Desktop.
Run shell command as grunt task, like `grunt $:/etc:ls:-laG`, or `grunt $::npm:pack`. Use it with `grunt.task.run('$:blah');`. $:opt_chdir_value:cmd_name:arg1:arg2
module.exports = function(grunt) {
'use strict';
var path = require('path');
var spawn = require('child_process').spawn;
grunt.registerTask('$', 'Run command in the form $::cmd:arg1:arg2, or $:/blah:cmd:arg.', function() {
var done = this.async();
var args = Array.prototype.slice.call(arguments);
var cwd = process.cwd();
var chdir = args.shift();
//make sure it's absolute
chdir = path.resolve(chdir);
// ALERT: I know, I know, bad practice, but it works for now
if (chdir && cwd !== chdir) {
grunt.log.writeln('From working directory:', path.relative(cwd, chdir).cyan);
process.chdir(chdir);
}
grunt.log.writeln('Spawn:', args.join(' ').cyan);
spawn(args.shift(), args, {stdio: 'inherit'})
.on('exit', function (code, signal) {
done(code != null || code < 1);
});
// Restore cwd
if (chdir && cwd !== chdir) process.chdir(cwd);
});
};
@drkibitz
Copy link
Author

drkibitz commented Dec 5, 2013

grunt.registerTask('installfoo', function () {
    grunt.task.run(['$::npm:install:foo']);
});

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