javascript - 即使任务失败,也可以在 grunt 中继续执行某些任务

标签 javascript gruntjs grunt-contrib-qunit

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

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

我可以做到这一点:

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

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

最佳答案

这是一种解决方法。虽然不太漂亮,但确实解决了问题。

您创建两个额外的任务,可以将它们包装在任何序列的开头/结尾,即使失败也可以继续执行。检查 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'
] );

我还提交了 feature request让 Grunt 原生支持这一点。

关于javascript - 即使任务失败,也可以在 grunt 中继续执行某些任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16612495/

相关文章:

javascript - 如何在 gruntfile.js 中放置 if else block

javascript - JQuery - 如果表格单元格包含数组中的数字,则更改表格单元格的颜色

javascript - D3 - 如何更新树布局中的隐藏分支

node.js - heroku上的咕噜声和 bower

javascript - Grunt + Concat + Angularjs

node.js - grunt、qunit、nodejs 后端测试

javascript - 如何在 JS 中检索以特定索引结尾的整个单词?

读取 url 的 JavaScript 从 html 页面获取特定的 div 标签并将其放置在另一个页面中。

coffeescript - CoffeeScript (eco) 模板中的 Grunt 图像路径