即使一个失败,也要继续执行grunt中的某些任务

Lar*_*n S 22 javascript gruntjs grunt-contrib-qunit

有没有办法配置一系列任务,以便即使一个任务失败,也可以运行特定的后续任务(我不希望整个批处理上的--force)?例如,考虑这样的情况

  1. 创建一些临时文件
  2. 运行一些涉及这些临时文件的单元测试
  3. 清理那些临时文件

我可以做这个:

grunt.registerTask('testTheTemp', ['makeTempFiles', 'qunit', 'removeTempFiles']);
Run Code Online (Sandbox Code Playgroud)

但是如果qunit失败,那么removeTempFiles任务永远不会运行.

exp*_*nit 18

这是一个解决方法.它并不漂亮,但确实解决了这个问题.

您创建了两个额外的任务,您可以在任何序列的开头/结尾处包装,即使失败也要继续.检查现有值grunt.option('force')是为了不覆盖--force从命令行传递的任何值.

grunt.registerTask('usetheforce_on',
 'force the force option on if needed', 
 function() {
  if ( !grunt.option( 'force' ) ) {
    grunt.config.set('usetheforce_set', true);
    grunt.option( 'force', true );
  }
});
grunt.registerTask('usetheforce_restore', 
  'turn force option off if we have previously set it', 
  function() {
  if ( grunt.config.get('usetheforce_set') ) {
    grunt.option( 'force', false );
  }
});
grunt.registerTask( 'myspecialsequence',  [
  'usetheforce_on', 
  'task_that_might_fail_and_we_do_not_care', 
  'another_task', 
  'usetheforce_restore', 
  'qunit', 
  'task_that_should_not_run_after_failed_unit_tests'
] );
Run Code Online (Sandbox Code Playgroud)

我还提交了Grunt 的功能请求,以便本地支持此功能.


Kyl*_*son 18

为了后人的缘故,这可能是一个改进的黑客攻击,而我们等待来自@explunit的公关到达咕噜声:

var previous_force_state = grunt.option("force");

grunt.registerTask("force",function(set){
    if (set === "on") {
        grunt.option("force",true);
    }
    else if (set === "off") {
        grunt.option("force",false);
    }
    else if (set === "restore") {
        grunt.option("force",previous_force_state);
    }
});

// .....

grunt.registerTask("foobar",[
    "task1",
    "task2",
    "force:on",     // temporarily turn on --force
    "task3",        // will run with --force in effect
    "force:restore",// restore previous --force state
    "task4"
]);
Run Code Online (Sandbox Code Playgroud)