JacobTheDev
JacobTheDev

Reputation: 18550

Pass an argument to a gulp task when another task depends on it

Let's say I have something like this:

var gulp = require("gulp"),
    argv = require("yargs").argv;

gulp.task("config", function (cb) {
    "use strict";

    if (argv.ftp) {
        console.log("Reading FTP settings...");
    } elseif (argv.sync) {
        console.log("Reading Sync settings..."):
    }
});

gulp.task("ftp", ["config"], function(cb) {
    console.log("FTPing...");
});

gulp.task("sync", ["config"], function(cb) {
    console.log("Syncing...");
});

Here's the idea:

Alternatively,

I have this all working just fine, except in order to get the config task working correctly as a dependency of either ftp or sync, I have to run gulp ftp --ftp or gulp sync --sync. What I'd like to do is tell gulp config that if it's being run as a dependency of gulp ftp, assume the --ftp flag, and it's being run as a dependency of gulp sync, assume the --sync flag.

So my question is, is there a way to tell when a gulp task is the dependency of another? When gulp-config runs due to being a dependency of gulp-ftp, is there a way to check that at the start of gulp-config?


UPDATE 1: I had the idea to do something like if (argv.ftp || config === "ftp"), where config is a global variable that gets set in either the ftp or sync task, but I then realized that won't work because the variable wouldn't get set until after the config task finished. I'll continue researching...

Upvotes: 0

Views: 607

Answers (2)

Sven Schoenung
Sven Schoenung

Reputation: 30574

What I'd like to do is tell gulp config that if it's being run as a dependency of gulp ftp, assume the --ftp flag, and it's being run as a dependency of gulp sync, assume the --sync flag.

You can actually kind of do this, although I am hesitant to recommend it since it is not part of the officially documented Gulp API.

Gulp inherits its task running capabilities from orchestrator which stores the execution order of tasks in an array seq. Running gulp ftp on the command line would result in a seq array ['config', 'ftp'].

So if config is run as a dependency of ftp, it will appear before ftp in the seq array. You can simply check for this condition in your config task:

gulp.task("config", function () {
  if (gulp.seq.indexOf('config') < gulp.seq.indexOf('ftp') || argv.ftp) {
    console.log("Reading FTP settings...");
  }
  if (gulp.seq.indexOf('config') < gulp.seq.indexOf('sync') || argv.sync) {
    console.log("Reading Sync settings...");
  }
});

Another option would be to check if ftp has been called on the command line. yargs puts non-option command line arguments into argv._:

gulp.task("config", function () {
  if (argv._.indexOf('ftp') >= 0 || argv.ftp) {
    console.log("Reading FTP settings...");
  }
  if (argv._.indexOf('sync') >= 0 || argv.sync) {
    console.log("Reading Sync settings...");
  }
});

However this will not work if ftp isn't explicitly invoked on the command line. This could happen if ftp itself is executed because another task depends on it.

Upvotes: 1

qtuan
qtuan

Reputation: 687

Here is my solution with refactoring:

var gulp = require("gulp");

function config(type) {
    "use strict";
    if (type == 'ftp') {
        console.log("Reading FTP settings...");
    } else if (type == 'sync') {
        console.log("Reading Sync settings...");
    }   
}

gulp.task("config-ftp", function () {
    config('ftp');
});

gulp.task("config-sync", function () {
    config('sync');
});

gulp.task("ftp", ["config-ftp"], function(cb) {
    console.log("FTPing...");
});

gulp.task("sync", ["config-sync"], function(cb) {
    console.log("Syncing...");
});

Upvotes: 0

Related Questions