Last active
December 24, 2015 02:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}; |
Author
drkibitz
commented
Dec 5, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment