2013-03-15 49 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); 
});