Vadorequest
Vadorequest

Reputation: 18079

How run grunt when server start from node?

I would like to refresh automatically my generated files, generated by grunt. To do so, I would like to run automatically grant from nodeJs.

I found the reverse, run node server from grunt, but it's not what I want to do.

Do you have tips to run grunt when the server start? Maybe it's something like call command line from node, but I'm not used to do that. Thanks.

Upvotes: 0

Views: 251

Answers (2)

Vadorequest
Vadorequest

Reputation: 18079

Final solution:

    var spawn = require('child_process').spawn;
    var cp = spawn(process.env.comspec, ['/c', 'grunt']);// ['/c', 'command', '-arg1', '-arg2']

    cp.stdout.on("data", function(data) {
        console.log(data.toString());
    });

    cp.stderr.on("data", function(data) {
        console.error(data.toString());
    });

Found here: Spawn on Node JS (Windows Server 2012) Thanks to @Diadara.

Upvotes: 2

Diadara
Diadara

Reputation: 591

You could use child process

var spawn = require('child_process').spawn,
grunt    = spawn('grunt', ['args']);

grunt.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

As you can see the child process is run asynchronously, if you want to run grunt synchronously then look at this question node.js execute system command synchronously

Upvotes: 1

Related Questions