有没有一种方法可以配置一系列任务,以便即使其中一个任务失败,特定后续任务(我不希望 --force 对整个批处理)也能运行?例如,考虑这样的情况
- 创建一些临时文件
- 运行一些涉及这些临时文件的单元测试
- 清理这些临时文件
我可以做到这一点:
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/