bruh
bruh

Reputation: 2305

Accessing Grunt Current Task from Another Task

Let's say I have one task, myCustomTask that gets called by another task, 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.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. In other words, the first condition is only met when we use this.nameArgs === 'myCustomTask'

How can I target whichever task, is currently calling our custom task so I can tell it to ignore it?

Upvotes: 1

Views: 88

Answers (1)

androberz
androberz

Reputation: 954

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

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.

Upvotes: 2

Related Questions