2013-03-15 19 views
19

我正在使用Grunt来编译CoffeeScript和Stylus以及一个监视任务。我也有我的编辑器(SublimeText)设置为保存文件,每次我离开他们(我讨厌失去工作)。你如何让默认的grunt.js不会在警告中崩溃?

不幸的是,如果Grunt在编译的任何文件中遇到语法错误,它会抛出警告并退出Aborted due to warnings。我可以通过传递--force来阻止它。有什么办法可以不中止默认行为(或者控制哪些任务的警告足够重要,可以退出Grunt?

回答

28

注册你自己的任务,它将运行你想要的任务,然后你必须通过force选项:

grunt.registerTask('myTask', 'runs my tasks', function() { 
    var tasks = ['task1', ..., 'watch']; 

    // Use the force option for all tasks declared in the previous line 
    grunt.option('force', true); 
    grunt.task.run(tasks); 
}); 
+3

这起作用,但然后强制选项对序列中的所有其余任务打开。我有另一个黑客在[这个问题]的答案(http://stackoverflow.com/questions/16612495/continue-certain-tasks-in-grunt-even-if-one-fails/16972894#16972894) – explunit 2013-06-06 22:01:19

+0

Couldn'你只是做grunt.option('force',false);运行任务后? – 2014-05-08 09:23:26

3

我试图asgoth的解决方案与Adam Hutchinson的建议,却发现强制标志正在设置回立即虚假读数grunt.task.run的grunt.task API文档,它指出

当前任务完成后,将按指定的顺序立即运行taskList中的每个指定任务。

这意味着我不能在调用grunt.task.run后马上将force标志设置回false。我找到的解决方案是有明确的任务将强制标志设置为false之后:

grunt.registerTask('task-that-might-fail-wrapper','Runs the task that might fail wrapped around a force wrapper', function() { 
    var tasks; 
    if (grunt.option('force')) { 
     tasks = ['task-that-might-fail']; 
    } else { 
     tasks = ['forceon', 'task-that-might-fail', 'forceoff']; 
    } 
    grunt.task.run(tasks); 
}); 

grunt.registerTask('forceoff', 'Forces the force flag off', function() { 
    grunt.option('force', false); 
}); 

grunt.registerTask('forceon', 'Forces the force flag on', function() { 
    grunt.option('force', true); 
});