-->

继续某些任务繁重的,即使一个失败(Continue certain tasks in grunt e

2019-09-04 02:49发布

是否存在一种配置的任务序列,使得特定的后续者(我不想--force对整批),即使一个失败运行的方法吗? 例如,考虑这样的情况下,

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

我可以做这个:

grunt.registerTask('testTheTemp', ['makeTempFiles', 'qunit', 'removeTempFiles']);

但是,如果再qunit失败removeTempFiles任务也永远不会运行。

Answer 1:

这里有一个解决方法。 这不是很漂亮,但它确实解决问题。

你创建你可以在你想继续即使在失败的任何序列的开始/结束包住两个额外的任务。 现有价值的检查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'
] );

我还提交了一份功能要求的咕噜声支持这一本身。



Answer 2:

为子孙后代着想,在我们等待这可能是一种改进的黑客是PR从@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"
]);


Answer 3:

也许你可以创建一个异步咕噜任务和grunt.util.spawn连续您想要的任务。 然后,你可以写成功/错误代码一些有条件的逻辑。 类似的事情在回答这个问题



Answer 4:

呼应马吕斯的评论中, 咕噜力任务的插件,现在提供这个功能。 通过上面的链接全部细节,但简而言之,这是你需要达到预期的效果是什么

npm install grunt-force-task --save-dev

然后将其导入到你的gruntfile

grunt.loadNpmTasks('grunt-force-task');

最后,只需添加力:前缀,你总是希望运行的一个面前的任务(一个或多个)。

grunt.registerTask('testTemp', ['makeTempFiles', 'force:qunit', 'removeTempFiles']);

现在,即使测试失败removeTempFiles将始终运行。



Answer 5:

一个问题与使用咕噜力任务的插件上面提到的是,咕噜过程将0现在无条件出口(这意味着通)。

如果你想在一个CI(持续集成)环境中使用呻吟,无法根据您的测试/编译(无论是CI的任务,这是一个问题qunit在OP)通过或失败。 我已经通过添加使用咕噜的新任务解决此问题的工作this.requires测试功能,测试是否qunit通过或失败:

grunt.registerTask('exitWithQunitStatus', function() {
  this.requires(['qunit']);
  return true;
})

grunt.registerTask('testTheTemp', ['makeTempFiles', 'force:qunit', 'removeTempFiles', 'exitWithQunitStatus']);

现在,如果qunit失败咕噜将与3,这表明退出Task Error 。 如果没有exitWithQunitStatus ,繁重的过程将0退出。

this.requires这里记载: http://gruntjs.com/api/inside-tasks#this.requires 。 基本上,除非所有指定“必需的”任务已经运行,并通过将失败的当前任务。



文章来源: Continue certain tasks in grunt even if one fails