如何防止grunt进程退出?

时间:2014-08-19 08:19:49

标签: javascript gruntjs

我想在我的插件中使用fs.watch。但是在运行任务和文件监视被停止后,grunt进程将退出。如何保持文件监视任务正常工作?我在grunt-contrib-watch

中找不到相似的代码 像这样的代码(我的插件的一部分):


      grunt.registerTask('prevent', 
         'watch file and prevent process exit',
         function(grunt){
           _fs = require('fs')
          _fs.watch(process.cwd(), function(event, filename){
            console.log(event, filename)
          }
     }

感谢您的投入!

1 个答案:

答案 0 :(得分:1)

如果您希望Grunt等待,您可以在任务函数中调用this.async

grunt.registerTask('prevent', 'watch file and prevent process exit',
    function (grunt) {
        var done = this.async(),
            _fs = require('fs');

        _fs.watch(process.cwd(), function (event, filename) {
            console.log(event, filename)
        });
    });

请参阅this.async doc here