2

I have a clean task that I want to finish before running my build tasks. No matter what I tried, I cannot get it to work properly: clean starts, build tasks starts, clean finishes (and throw an error because some folder is being written to) while build tasks are still running.

Here's some of my task definitions:

gulp.task('default', ['clean', 'build']);
gulp.task('js', ['phnxjs', 'vendorsjs']);
gulp.task('css', ['phnxcss', 'vendorscss']);
gulp.task('assets', ['fonts', 'images', 'templates']);
gulp.task('build', ['js', 'css', 'assets', 'login']);
gulp.task('index', ['build'], index); <-- index is a function defined elsewhere

gulp.task('clean', function() {
    return gulp.src('**/*', targetOptions)
        .pipe(filters.excludeWebInf)
        .pipe(g.rimraf());
});

ALL my tasks returns a stream

I also tried calling the callback function from the clean task instead of returning a stream, to no avail.

I even gave run-sequence a try, like so:

gulp.task('default', function() {
    run('clean', 'build', 'index');
});

It seems to yields better results but looks like it's missing a step as index isn't executed


edit: Tried tons of "task dependency" combinations, amongst which I tried to have task('build', ['clean', 'js' ...]

Still won't work with that scenario:

[13:46:32] Starting 'clean'...
[13:46:32] Starting 'phnxjs'...                   <-------- Clean not finished, other tasks started
[13:46:33] Starting 'vendorsjs'...
[13:46:33] Starting 'phnxcss'...
[13:46:33] Starting 'vendorscss'...
[13:46:33] Starting 'fonts'...
[13:46:33] Starting 'images'...
[13:46:33] Starting 'templates'...
[13:46:33] Starting 'login'...
[13:46:33] Starting 'index'...
[13:46:34] Finished 'vendorscss' after 1.2 s
[13:46:34] Finished 'login' after 1.1 s
[13:46:34] Finished 'index' after 1.09 s
[13:46:35] Finished 'fonts' after 2.7 s
[13:46:35] Finished 'images' after 2.7 s
[13:46:35] 'clean' errored after 3.1 s             <--------- Clean Error
[13:46:35] Error in plugin 'gulp-rimraf' 
ENOTEMPTY, rmdir '/Users/oclement/Documents/phoenix/MedifastCerBrokenDown/MedifastWeb/build/WebContent/app/generated'
[13:46:35] Finished 'templates' after 2.72 s
[13:46:36] Finished 'phnxcss' after 3.46 s
Olivier Clément
  • 764
  • 1
  • 8
  • 27
  • http://stackoverflow.com/questions/24342030/gulp-synchronicity-without-dependency/24342064#24342064 – coma Jul 30 '14 at 17:43
  • 1
    possible duplicate of [Gulp. can't figure how to run tasks synchronously after each other](http://stackoverflow.com/questions/22824546/gulp-cant-figure-how-to-run-tasks-synchronously-after-each-other) – Evan Davis Jul 30 '14 at 17:43
  • This is probably the most common Gulp question on SO. – Evan Davis Jul 30 '14 at 17:43
  • 1
    I did saw these questions and answers, and I did try all these solutions, to no avail. – Olivier Clément Jul 30 '14 at 17:52
  • @OlivierClément did you figure it out? – Dominic May 21 '15 at 22:03
  • @DominicTobias Havent played in there in a while, but long story short, the tasks defined in the array (gulp.task('name', ['list', 'of', 'tasks], function() {});) are all executed in parallel. So you have to work around this. Putting: ['clean', 'build'] while have both task running at the same time, which obviously does't make sense – Olivier Clément May 22 '15 at 20:03
  • @OlivierClément Thanks I ended up using run-sequence, perhaps it wasn't working for you because I am passing the function arg `cb` as the last param of the run call? – Dominic May 22 '15 at 20:40

2 Answers2

0

build needs to have clean as a dependency.

// -----------------v
gulp.task('build', ['clean', 'js', 'css', 'assets', 'login']);
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
0

You can use the following code...

  gulp.task( 'task1', () => console.log(a) )
  gulp.task( 'task2', () => console.log(a) )
  gulp.task( 'task3', () => console.log(a) )
  gulp.task( 'task4', () => console.log(a) )
  gulp.task( 'task5', () => console.log(a) )

  function runSequential( tasks ) {
    if( !tasks || tasks.length <= 0 ) return;

    const task = tasks[0];
    gulp.start( task, () => {
        console.log( `${task} finished` );
        runSequential( tasks.slice(1) );
    } );
  }
  gulp.task( "run-all", () => runSequential([ "task1", "task2", "task3", "task4", "task5" ));
Assaf Moldavsky
  • 1,681
  • 1
  • 19
  • 30