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 mand
I'm pretty new to Grunt, so I'm probably missing something obvious. I suspect the mand 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.
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 mand
I'm pretty new to Grunt, so I'm probably missing something obvious. I suspect the mand 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.
Share Improve this question edited Jul 9, 2013 at 14:57 Adam Terlson asked Jul 9, 2013 at 14:47 Adam TerlsonAdam Terlson 12.7k4 gold badges45 silver badges63 bronze badges4 Answers
Reset to default 10Update
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 remend trying to get this to work.
Ok, I'll just answer my own question. I was right, the mand I had was wrong.
Updated code:
var grunt = require('grunt');
grunt.registerTask('default', 'Log some stuff.', function() {
console.log('stuff');
});
grunt.tasks(['default']);
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);
}
});
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
.
We're using Jenkins for our builds. So here is how we resolved the issue using bash:
#!/bin/bash
export PATH=$PATH:/usr/local/bin
grunt full-build | tee /dev/stderr | awk '/Aborted/ || /Fatal/{exit 1}'
echo rv: $?
exit $?
The use of /dev/stderr is because we're running within Jenkins and we still want to ouput to show up within the console.
Visualjeff