[英]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. 我有一个启动IIS Express异步的任务,并且要停止IIS,我必须触发一个grunt事件。
I would like to make a task that just waits until i press ctrl-c
and then fires that event. 我想做一个等待直到按ctrl-c
然后触发该事件的任务。
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. 该任务成功停止了grunt,但没有正确发送事件。
SIGINT
handlers work in Node.js, but not in Grunt (I don't know why). SIGINT
处理程序在Node.js中工作,但在Grunt中不起作用(我不知道为什么)。 I handle ctrl+c
manually using readline
module and listen to exit
event: 我使用readline
模块手动处理ctrl+c
并监听exit
事件:
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). 另外我建议收听SIGINT
, SIGHUP
和SIGBREAK
信号以处理控制台窗口关闭或ctrl+break
(如果有人使用它,呵呵)。 Call process.exit()
in these handlers when you also want to stop the app: 当您还要停止应用程序时,在这些处理程序中调用process.exit()
:
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 . 我有一个grunt-iisexpress
,在退出时杀死IIS: https : //github.com/whyleee/grunt-iisexpress 。
I tried whyleee's suggestion, but I found that grunt
wasn't waiting for the cleanup process to exit before shutting itself down. 我尝试了whyleee的建议,但我发现grunt
没有等待清理过程退出,然后关闭自己。
The solution for me was based on this article . 我的解决方案基于这篇文章 。
module.exports = function(grunt) {
var exec, readline, shuttingDown, stdInterface;
shuttingDown = false;
readline = require("readline");
exec = require("child_process").exec;
// other grunt requires / task loads, including a "cleanup" task ...
stdInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
stdInterface.on("SIGINT", function() {
var child;
if (shuttingDown) {
return;
}
shuttingDown = true;
console.info("Cleaning up ...");
child = exec("grunt cleanup", function(err, stdout, stderr) {
console.info(stdout);
process.exit(err ? 1 : 0);
});
});
// other grunt config ...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.