1

Objective

Create a gulp.watch task to run others tasks in a specific order

Why this is not a duplicate

Many people have suggested I take a look at How to run Gulp tasks sequentially one after the other. However, this question focuses on gulp.task while my question focuses on gulp.watch.

The solution I am searching is a way to use gulp.watch to achieve the synchronous effect I am after. If this is not possible however, I will fall-back to gulp.task.

Background

I have a small project, I have some test files as well as the the gulp gulp-complexity plugin. I wish to run the test files first, and then run the gulp-complexity plugin every time a JavaScript file is changed.

What I have tried

I read the documentation and saw the following tutorials:

They were very good, but I still don't understand how I can have a gulp.watch task that runs tasks in synchronously (in a specific order) instead of running them asynchronously.

Code

This is what I got so far:

var gulp = require("gulp");
var mocha = require("gulp-mocha");
var complexity = require("gulp-complexity");

gulp.task("test", function(){
    gulp
    .src("./test.js")
    .pipe(mocha())
    .on("error", function(){
        this.emit("end");
    });
});


gulp.task("complexity", ["test"], function(){
    gulp.src('*.js').pipe(complexity());
});

gulp.task("watch", function(){
    gulp.watch("./*.js", ["test", "complexity"]);
});

What am I doing wrong here?

Please bare in mind I am still very new to gulp (started learning it today!) so any explanations on my problem would be welcome!

Community
  • 1
  • 1
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 2
    Possible duplicate of [How to run Gulp tasks synchronously/one after the other](http://stackoverflow.com/questions/22824546/how-to-run-gulp-tasks-synchronously-one-after-the-other) – Sven Schoenung Jun 22 '16 at 14:25
  • i read that question and it didn't help because they don't use gulp.watch. Thanks for the suggestion thogh ! – Flame_Phoenix Jun 22 '16 at 14:39
  • Edited my question to explain why it is not a duplicate! – Flame_Phoenix Jun 22 '16 at 14:44
  • 2
    Whether you use `gulp.watch()` or not doesn't matter. The answers in that question work for your case as well. – Sven Schoenung Jun 22 '16 at 14:49
  • it matters to me. that's why I made the question :D – Flame_Phoenix Jun 22 '16 at 14:54
  • @Flame_Phoenix, Sven is correct. The same answer for the `gulp.task()` thread applies to yours, because both `gulp.task` and `gulp.watch` accept an array of tasks to run that execute in exactly the same manner, essentially making the task and watch methods identical in terms of how they run tasks (whether you accept that or not). Your question is indeed a duplicate. – sgarcia.dev Jun 23 '16 at 03:53
  • So, what is the difference between the two? They share the same bug? If you could an elaboration on that, I would pick your answer! – Flame_Phoenix Jun 23 '16 at 07:07

1 Answers1

3

This is a Gulp issue and should be be fixed in gulp 4.0

For now, try run-sequence [ https://www.npmjs.com/package/run-sequence ]

v-andrew
  • 21,939
  • 6
  • 36
  • 41