Running a Grunt Task Directly from Node

How to execute a Grunt task directly from Node without a crawl in the CLI?

I have the following "POC" code; however, “material” is never recorded.

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 new to Grunt, so I probably missed something obvious. I suspect that the command I use to “start” the task simply puts it in the queue and does not actually start the work. However, I can not find the documentation for manual start.

+7
javascript gruntjs
source share
4 answers

Update

While this is the answer, Grunt has problems launching directly from Node. Not the last of them is when the grunt task does not work, it calls process.exit and beautifully terminates your node instance. I cannot recommend this to work.


Ok, I’ll just answer my question. I was right, I had the wrong team.

Updated code:

 var grunt = require('grunt'); grunt.registerTask('default', 'Log some stuff.', function() { console.log('stuff'); }); grunt.tasks(['default']); 
+10
source share

it takes a lot of time, and finally I already did it 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); } }); 
+4
source share

What if you do the following?

 grunt.tasks("default"); 

I created a Grunt runner in one of my projects that parses and then calls the line above. Almost what you already answered, but with support for Gruntfile.js .

+1
source share

We use Jenkins for our collections. So, here is how we solved the problem with bash:

 #!/bin/bash export PATH=$PATH:/usr/local/bin grunt full-build | tee /dev/stderr | awk '/Aborted/ || /Fatal/{exit 1}' echo rv: $? exit $? 

Using / dev / stderr is due to the fact that we are working in Jenkins and we still want to output it to the console.

Visualjeff

-one
source share

All Articles