Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Grunt Task directly from Node

How do I execute a Grunt task directly from Node without shelling out to the CLI?

I've got the following "POC" code; however, "stuff" is never logged.

var grunt = require('grunt');

grunt.registerTask('default', 'Log some stuff.', function() {
    console.log('stuff');
});

grunt.task.run('default'); // This is probably not the right command

I'm pretty new to Grunt, so I'm probably missing something obvious. I suspect the command I'm using to "run" the task is just queuing it, and doesn't actually start running things. I can't find documentation for manually running things, though.

like image 228
Adam Terlson Avatar asked Jul 09 '13 14:07

Adam Terlson


3 Answers

Update

While this is the answer, Grunt has tons of issues with being run directly from Node. Not the least of which is when the grunt task fails, it calls process.exit and nicely quits your node instance. I cannot recommend trying to get this to work.


Ok, I'll just answer my own question. I was right, the command I had was wrong.

Updated code:

var grunt = require('grunt');

grunt.registerTask('default', 'Log some stuff.', function() {
    console.log('stuff');
});

grunt.tasks(['default']);
like image 149
Adam Terlson Avatar answered Sep 27 '22 01:09

Adam Terlson


it take much time and finally, I already made it working for me

   var util = require('util')
    var exec = require('child_process').exec;
    var child = exec("/usr/local/bin/grunt --gruntfile /path/to/Gruntfile.js", function (error, stdout, stderr) {
      util.print('stdout: ' + stdout);
      util.print('stderr: ' + stderr);
      if (error !== null) {
        console.log('exec error: ' + error);
      }
    });
like image 44
Chung Nguyen Avatar answered Sep 25 '22 01:09

Chung Nguyen


What if you do the following?

grunt.tasks("default");

I've created an Grunt runner in one of my projects that does some parsing and then call the line above. Almostly what you already answered, but with support for a Gruntfile.js.

like image 22
gustavohenke Avatar answered Sep 26 '22 01:09

gustavohenke