How can I check to see if a task is ran as a dependency of another task in gulp@4?

166 views Asked by At

I use gulp-notify to trigger notifications when tasks complete. If a task is ran standalone, a notification for that specific task is triggered. If a task is ran as a dependency of another task, a notification for all dependencies is triggered.

In gulp@3, I check if the task is being called as a dependency using gulp.seq, which contains an array of the tasks being ran. Let's say I have three tasks: default, styles, and scripts, with the later two set as dependencies of the first. When running gulp styles, gulp.seq will contain [ 'styles' ]. When running gulp (the default task), gulp.seq will contain [ 'styles', 'scripts', 'default' ]. Knowing that, I then check gulp.seq.indexOf("styles") > gulp.seq.indexOf("default"), which tells me weather or not styles was ran as part of the default task.

With gulp@4, it appears that gulp.seq no longer exists. I've tried digging through the documentation and source code with no luck. It seems like gulp.tree({ deep:true }) (docs) might be what I'm looking for, but I don't see anything in it that returns anything useful.

Is there an equivalent of gulp.seq in gulp@4?

2

There are 2 answers

1
JacobTheDev On BEST ANSWER

Through a bit of luck, I discovered this was possible via the module yargs, which I already have installed.

When running gulp styles, for example, I can check argv._.indexOf("styles") > -1, as it contains ['styles']. When running gulp (i.e the default task), it contains []. In my testing, this works perfectly for my use case.

2
Harshal Patil On

The API gulp.seq was never an official prop exposed by Gulp. With Gulp 4, you cannot do that. gulp.tree({ /* */ }) will not solve this problem for you.

Having said that, if you still need to find whether a task has run during some other task's pipeline, then you will have to decorate every gulp task with your own wrapper using something like this:

let runTasks = [];

function taskWrapper(taskName, tasks, thisTask) {

    let callbackTask;

    function innerCallback(cb) {
        runTasks.push(taskName);
        cb();
    }

    if (thisTask) {
        callbackTask = function(cb) {
            thisTask(function () {
                innerCallback(cb);
            });
        }
    } else {
        callbackTask = innerCallback;
    }

    const newTasks = [ ...tasks, callbackTask ];

    gulp.task(taskName, gulp.series(newTasks));
}

// INSTEAD OF THIS
// gulp.task('default', gulp.series('style', 'script', function () { }));

// DO THIS
taskWrapper('default', ['style', 'script'], function(cb) {
    console.log('default task starting');
    cb();
});

NOTE: Above code snippets has limitation. If you use watch mode, array maintaining the executed tasks i.e. runTasks will keep on growing. Also, it assumes tasks will always run in series. For a parallel mode, the logic gets little complicated.

Finally, you can also have a predefault task to help it further:

taskWrapper('predefault', [], function(cb) {
    // RESET runTasks
    runTasks = [];
    cb();
});

taskWrapper('default', ['predefault', 'style', 'script'], function(cb) {
    console.log('default task starting');
    cb();
});

Also, I am doubtful if gulp-notify will work with Gulp 4.