Alan2
Alan2

Reputation: 24602

How can I run three tasks with parameters in sequence?

I have this code so far:

gulp.task('make_prod_aa', function () {
    makeAppHtml(config.srcAalHtml, function () {
        runSequence(
          'makeTemplate',
          'make_css_bundle',
          'rename_css_bundle',
          'make_js_bundle',
          'rename_js_bundle',
          function () {
              makeAppIndex('index-aa.html');
          });
    });
});

It allows me to pass parameters into two functions and runs the functions and the tasks all in order. Now I would like to pass parameters into a third task. I want to be able to pass the config.aaTemplates parameter to the task makeTemplate which is here:

gulp.task('makeTemplate', function () {
    return gulp.src(config.aaTemplates)
          .pipe(print(function (file) {
              return "Added file " + file + " to template";
          }))
       .pipe(minifyHTML({ collapseWhitespace: true }))
       .pipe(templateCache({ standalone: true, root: '/app' }))
       .pipe(gulp.dest(config.destPartials))
       .pipe(gzip(gzip_options))
       .pipe(gulp.dest(config.destPartials));
});

Would appreciate any suggestions on how I can do this.

Upvotes: 0

Views: 628

Answers (1)

Yevgen Safronov
Yevgen Safronov

Reputation: 4033

I don't think there is another option than using JS closure to have all tasks which run in a sequence sharing the same config. Proof

For example:

var config = require('path/to/config');

function makAppAppHtml () {
        runSequence(
          'makeTemplate',
          'make_css_bundle',
          'rename_css_bundle',
          'make_js_bundle',
          'rename_js_bundle',
          function () {
              makeAppIndex('index-aa.html');
          });
}

function makeTemplate () {
    return gulp.src(config.aaTemplates)
            .pipe(print(function (file) {
                return "Added file " + file + " to template";
            }))
         .pipe(minifyHTML({ collapseWhitespace: true }))
         .pipe(templateCache({ standalone: true, root: '/app' }))
         .pipe(gulp.dest(config.destPartials))
         .pipe(gzip(gzip_options))
         .pipe(gulp.dest(config.destPartials));
  });
}

gulp.task('make_prod_aa', makAppAppHtml);
gulp.task('makeTemplate', makeTemplate);

Both makeAppHtml and makeTemplate have access to config object because it was defined in the outer scope, the same trick could be applied to the function scope, for more detailed example I recommend JavaScript Garden article.

Upvotes: 2

Related Questions