如何使grunt.js默认情况下不崩溃警告?

19

我正在使用Grunt编译CoffeeScript和Stylus,并使用监视任务。 我还将我的编辑器(SublimeText)设置为每次离开页面时保存文件(我讨厌丢失工作)。

不幸的是,如果Grunt在任何要编译的文件中出现语法错误,它会抛出警告并带有“由于警告而中止”的退出信息。 通过传递 --force,可以防止它这样做。 是否有方法使不中止成为默认行为(或控制哪些任务的警告足以退出Grunt)?

2个回答

29

注册您自己的任务,以运行您想运行的任务。然后您必须传递 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
这个方案可行,但是接下来序列中的所有剩余任务都会开启“force”选项。我在这个问题的答案中提供了另一个方法。 - explunit
在运行任务后,你不能只执行grunt.option('force', false)吗? - Adam Hutchinson

3
我尝试了asgoth的解决方案,并结合Adam Hutchinson的建议,但发现强制标志立即被设置回false。在阅读grunt.task API文档中关于grunt.task.run的说明时,它指出:

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

这意味着我不能简单地在调用grunt.task.run后立即将强制标志设置为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);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接