How do you make grunt.js not crash on warnings by default?

ぃ、小莉子 提交于 2019-12-09 05:08:55

问题


I'm using Grunt to compile CoffeeScript and Stylus with a watch task. I also have my editor (SublimeText) set to save files every time I page away from them (I hate losing work).

Unfortunately, if Grunt hits a syntax error in any of the files it's compiling, it throws a warning and quits with Aborted due to warnings. I can stop it doing this by passing --force. Is there any way to make not aborting the default behavior (or control which tasks' warnings are important enough to quit Grunt?


回答1:


Register your own task, which will run the tasks you want. Then you have to pass the force option:

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);
});



回答2:


I tried asgoth's solution with Adam Hutchinson's suggestion, but found that the force flag was being set back immediately to false. Reading the grunt.task API docs for grunt.task.run, it states that

Every specified task in taskList will be run immediately after the current task completes, in the order specified.

Which meant that I couldn't simply set the force flag back to false immediately after calling grunt.task.run. The solution I found was to have explicit tasks setting the force flag to false afterwards:

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);
});


来源:https://stackoverflow.com/questions/15423851/how-do-you-make-grunt-js-not-crash-on-warnings-by-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!