Running a command in a Grunt Task

JavascriptTemplatesGruntjsContinuous IntegrationGoogle Closure-Compiler

Javascript Problem Overview


I'm using Grunt (task-based command line build tool for JavaScript projects) in my project. I've created a custom tag and I am wondering if it is possible to run a command into it.

To clarify, I'm trying to use Closure Templates and "the task" should call the jar file to pre-compile the Soy file to a javascript file.

I'm running this jar from command line, but I want to set it as a task.

Javascript Solutions


Solution 1 - Javascript

Alternatively you could load in grunt plugins to help this:

grunt-shell example:

shell: {
  make_directory: {
    command: 'mkdir test'
  }
}

or grunt-exec example:

exec: {
  remove_logs: {
    command: 'rm -f *.log'
  },
  list_files: {
    command: 'ls -l **',
    stdout: true
  },
  echo_grunt_version: {
    command: function(grunt) { return 'echo ' + grunt.version; },
    stdout: true
  }
}

Solution 2 - Javascript

Check out grunt.util.spawn:

grunt.util.spawn({
  cmd: 'rm',
  args: ['-rf', '/tmp'],
}, function done() {
  grunt.log.ok('/tmp deleted');
});

Solution 3 - Javascript

I've found a solution so I'd like to share with you.

I'm using grunt under node so, to call terminal commands you need to require 'child_process' module.

For example,

var myTerminal = require("child_process").exec,
    commandToBeExecuted = "sh myCommand.sh";

myTerminal(commandToBeExecuted, function(error, stdout, stderr) {
    if (!error) {
         //do something
    }
});
    

Solution 4 - Javascript

If you are using the latest grunt version (0.4.0rc7 at the time of this writing) both grunt-exec and grunt-shell fail (they don't seem to be updated to handle the latest grunt). On the other hand, child_process's exec is async, which is a hassle.

I ended up using Jake Trent's solution, and adding shelljs as a dev dependency on my project so I could just run tests easily and synchronously:

var shell = require('shelljs');

...

grunt.registerTask('jquery', "download jquery bundle", function() {
  shell.exec('wget http://jqueryui.com/download/jquery-ui-1.7.3.custom.zip');
});

Solution 5 - Javascript

Guys are pointing child_process, but try to use execSync to see output..

grunt.registerTask('test', '', function () {
        var exec = require('child_process').execSync;
        var result = exec("phpunit -c phpunit.xml", { encoding: 'utf8' });
        grunt.log.writeln(result);
});

Solution 6 - Javascript

For async shell commands working with Grunt 0.4.x use https://github.com/rma4ok/grunt-bg-shell.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuanOView Question on Stackoverflow
Solution 1 - JavascriptpapercowboyView Answer on Stackoverflow
Solution 2 - JavascriptNick HeinerView Answer on Stackoverflow
Solution 3 - JavascriptJuanOView Answer on Stackoverflow
Solution 4 - JavascriptkikitoView Answer on Stackoverflow
Solution 5 - JavascriptArtjom KurapovView Answer on Stackoverflow
Solution 6 - JavascriptDaniel SteigerwaldView Answer on Stackoverflow