0

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.

Vadorequest
  • 16,593
  • 24
  • 118
  • 215

2 Answers2

2

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.

Community
  • 1
  • 1
Vadorequest
  • 16,593
  • 24
  • 118
  • 215
1

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

Community
  • 1
  • 1
Diadara
  • 591
  • 1
  • 3
  • 20
  • Yes, but I try before ;) And I have some trouble under windows OS. https://github.com/joyent/node/issues/2318 or https://www.google.dk/search?q=spawn+enoent&oq=spawn+enoent&aqs=chrome..69i57.3004j0j1&sourceid=chrome&ie=UTF-8#q=spawn+enoent+windows&safe=off – Vadorequest Dec 01 '13 at 04:51
  • Your solution is OK under !windows (mac & linux work) – Vadorequest Dec 01 '13 at 04:53