1

I use Gulp 4 and I have this task:

gulp.task('deploy', gulp.series('clean', 'handlers:all', 'deployment'));

The task call three other tasks:

  1. Task clean: removes the build folder.
  2. Task handlers:all: re-creates the build folder and adds files there.
  3. Task deployment: transfers the contents of the build folder to another location

I have a problem with task the deployment which look something like this:

gulp.task('deployment', done => {
    gulp.src('./build/')
        .pipe(gulp.dest('../other-project/'));
});

gulp.src() can't find the build folder because it was deleted by the task clean. When I set the timeout there are no problems. But this solution is bad.

gulp.task('deployment', done => {
    setTimeout(() => {
        gulp.src('./build/')
            .pipe(gulp.dest('../other-project/'));
    }, 2000);
});

How do I solve this problem?

Task clean:

gulp.task('clean', done => {
    del('./build/');
    done();
});

Task handlers:all:

gulp.task('handlers:all', gulp.parallel('scripts', 'templates', 'styles', 'fonts', 'favicons', 'images', 'svg', 'dist'));
  • can you post the `handlers:all` script as well? – AlexScr Apr 02 '18 at 12:33
  • @AlexScr these are just parallel tasks: `gulp.task('handlers:all', gulp.parallel('scripts', 'templates', 'styles', 'fonts', 'favicons', 'images', 'svg', 'dist'));` – dmitryshishkin Apr 02 '18 at 12:39
  • Post your clean as well. – Nick Apr 02 '18 at 13:21
  • @Nick I added this to the question. – dmitryshishkin Apr 02 '18 at 13:30
  • Quick note: hopefully you have return statements or are calling the done() function in all your tasks. You don't in your original "deployment' task. I assume one or more of the tasks in your 'handlers:all' is recreating the build folder: gulp.dest('./build'). Second, there is no reason to use './build/' just use './build'. – Mark Apr 02 '18 at 13:59
  • Agree with @Mark. Seems like `clean & handlers:all` tasks are async, and `gulp` doesn't know when to start next task. So you may want to use a callback function `done` once you removed all folders. Plus you should do same things for your `handlers:all` task as well. Just to make sure that it runs like a `sync` but it's actually `async` flow. – The Reason Apr 02 '18 at 14:01

1 Answers1

1

Try to replace your clean task to the next one:

gulp.task('clean', done => {
  del('./build/').then(() => done());
});

It needs because of del package returns a promise and you should tell gulp to wait till it will be resolved. The other option can be del.sync.

I'm not sure about rest of tasks but it should follow the same process. If you have any async tasks - call done in the right place.

It might be useful for you async vs sync

Hope it makes sense.

The Reason
  • 7,705
  • 4
  • 24
  • 42