[英]Accessing Grunt Current Task from Another Task
Let's say I have one task, myCustomTask
that gets called by another task, quick
. 假设我有一个任务, myCustomTask
被另一个任务quick
调用。
grunt.registerTask('myCustomTask', function(){
if(this.nameArgs === 'quick'){
console.log('ignoring task...');
return;
}else{
grunt.task.run('my-custom-task');
}
});
I would like myCustomTask
to be ignored by grunt quick
, without removing it from the quick
task. 我希望grunt quick
可以忽略myCustomTask
, 而不必将其从quick
任务中删除。
grunt.registerTask('quick', [
'task1',
'task2',
'myCustomTask'
]);
I was hoping this.nameArgs === 'quick'
would trigger when the task is being ran as grunt quick
but it skips that and reaches the else block. 我希望this.nameArgs === 'quick'
会在grunt quick
运行任务时触发,但是会跳过该任务并到达else块。 In other words, the first condition is only met when we use this.nameArgs === 'myCustomTask'
换句话说,仅当我们使用this.nameArgs === 'myCustomTask'
时才满足第一个条件this.nameArgs === 'myCustomTask'
How can I target whichever task, is currently calling our custom task so I can tell it to ignore it? 如何定位当前正在调用我们的自定义任务的任何任务,以便我可以告诉它忽略它?
Have you tried Dynamic alias tasks ? 您是否尝试过动态别名任务 ?
I think you can do the following: 我认为您可以执行以下操作:
grunt.registerTask('myCustomTask', function(mode) {
if (mode == 'quick') {
console.log('ignoring task...');
return;
}
grunt.task.run('my-custom-task');
});
UPDATE UPDATE
If you want to exclude task execution if some task had been ran then you can do the following: 如果要在执行某些任务时排除任务执行,则可以执行以下操作:
var taskSettings = {};
grunt.registerTask('task1', function() {
console.log('task 1');
});
grunt.registerTask('task2', function() {
if ('quick' == taskSettings.origin) {
console.log('skip task 2');
} else {
console.log('task 2');
}
});
grunt.registerTask('quick', function () {
taskSettings.origin = this.name;
grunt.task.run('task1', 'task2');
});
But as for me it looks strange and I would suggest splitting logic among task's targets. 但是就我而言,这看起来很奇怪,我建议在任务目标之间分配逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.