Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt task to wait for exit

I've got a task that starts IIS Express async, and to stop IIS I have to fire a grunt event.

I would like to make a task that just waits until i press ctrl-c and then fires that event.

I've tried doing this:

grunt.registerTask("killiis", function(){
    process.stdin.resume();
    var done = this.async();
    grunt.log.writeln('Waiting...');    

    process.on('SIGINT', function() {
        grunt.event.emit('iis.kill');
        grunt.log.writeln('Got SIGINT.  Press Control-D to exit.');
        done();
    });
});

The task stops grunt successfully, but doesn't send the event properly.

like image 517
MrJD Avatar asked Nov 19 '25 08:11

MrJD


1 Answers

SIGINT handlers work in Node.js, but not in Grunt (I don't know why). I handle ctrl+c manually using readline module and listen to exit event:

var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.on('SIGINT', function() {
  process.emit('SIGINT');
});

process.on('exit', killIis);

function killIis() {
  // kill it
}

Additionally I suggest to listen to SIGINT, SIGHUP and SIGBREAK signals to handle console window close or ctrl+break (if anybody use it, heh). Call process.exit() in these handlers when you also want to stop the app:

process.on('exit', killIis);
process.on('SIGINT', killIisAndExit);
process.on('SIGHUP', killIisAndExit);
process.on('SIGBREAK', killIisAndExit);

I have a fork of grunt-iisexpress that kills IIS on exit: https://github.com/whyleee/grunt-iisexpress.

like image 79
whyleee Avatar answered Nov 20 '25 21:11

whyleee