8

I am getting into the many streams of Gulp and have run across a confounding subject. I'd like to post a notification when all the tasks are actually complete. I see that the tasks are executed but running asynchronously by default.

What if I want to show display a notification after each step is complete...and at the end when all steps are complete?

What is the best way to gain more control over the timing of tasks in gulp?

Currently, I'm using gulp-notify to display notifications.

UPDATE X 2

I'm not really having any errors, but would like to better understand the order of operations here and how I can trigger my own notification of when all tasks have been complete. Here is the example.

var gulp = require('gulp'),
    gp_concat = require('gulp-concat'),
    gp_notify = require('gulp-notify');

gulp.task('task1', function() {
    return gulp.src(['file1.js','file2.js'])
        .pipe(gp_concat('file1_2.js')
        .pipe(gp_notify({ message: "file1_2 created." }
})

gulp.task('task2', function() {
    return gulp.src(['file3.js','file4.js'])
        .pipe(gp_concat('file3_4.js')
        .pipe(gp_notify({ message: "file3_4 created." }
})

gulp.task('mainTask', ['task1','task2'], function() {
    gulp.src('file*_*.js')
        .pipe(gp_notify({ message: "All tasks complete." }))
});

In the console, the notifications are now timed correctly, however at the end of the execution, right before Finished 'mainTask' after xx ms the final 'All tasks complete' message fires off [n-1] times, where n is the count of sub tasks.

What is causing this final notification to get triggered so many times and how can that be suppressed?

beauXjames
  • 8,222
  • 3
  • 49
  • 66

2 Answers2

10

I guess you tried to append a call to gulp-notify at the end of every task and one to the end of a general task who depends on the previous ones.

So the problem probably is related to the tasks not communicating correctly their "finished status" to the main task. For this topic you can check the Running tasks in series, i.e. Task Dependency recipe or the always handy run-sequence package.

Update

In order to let a task know that its subtasks are done is to make them return a stream so in the example code the subtasks can simply return the gulp.src pipe.

gulp-notify when called with a string message will output that message for every file present in the passed stream so, in the final task of the example, it will be called twice. The call to gp_notify should be changed to: gp_notify({ message: "All tasks complete.", onLast: true }). This way the message will be notified only for the last file.

Ghidello
  • 1,863
  • 20
  • 21
  • Yea...the problem is that the notifications would get run before the tasks were actually complete...and, for instance, the notification for the main task gets triggered before the sub-task...I did briefly read on implementing promises in gulp, but am not certain how that would work in the 'pipe'. – beauXjames Oct 29 '14 at 14:15
  • k...everything is working, but updated to help you visualize what I am trying to ask – beauXjames Oct 29 '14 at 16:16
  • You should place a **return** before the `gulp.src` call in your **task1** and **task2** tasks so they'll notify to the calling task that they're completed. Every gulp task notifies their completeness returning a stream or calling a collback that the caller may provide. So, in your example, the return statement should solve the issue. – Ghidello Oct 29 '14 at 16:43
  • so I added the return and it did change things...I was notified after each...however, the notification in 'mainTask' was triggered [n-1] times AFTER it's finished state was reached. – beauXjames Oct 30 '14 at 16:54
  • Actually I don't know how the gulp-notify plugin is supposed to work. Anyway, still speaking about your sample above, `gp_notify` will receive the stream created by `gulp.src` which will contain 2 files ['file1_2.js', 'file3_4.js']. I guess that it will print your output message "**All tasks complete.**" twice but again, i'm just guessing. – Ghidello Oct 30 '14 at 21:36
  • I gave a quick look to the plugin home page. You should change your call to: gp_notify({ message: "All tasks complete."**, onLast: true** }). This way the message will be notified only for the last file in the stream instead of being notified for each file. – Ghidello Oct 30 '14 at 21:45
  • Thanks...this helps, but I'm going to have to start a more general question about the order of operations in Gulp...I'm not understanding out things are being processed. – beauXjames Nov 04 '14 at 17:08
9

By default, gulp-notify sends notifications for each file in the stream. If you only want one notification per-stream, add onLast: true to the options passed to notify(). e.g.

var gulp = require('gulp'),
    gp_concat = require('gulp-concat'),
    gp_notify = require('gulp-notify');

gulp.task('task1', function() {
    return gulp.src(['file1.js','file2.js'])
        .pipe(gp_concat('file1_2.js'))
        .pipe(gp_notify({ message: "file1_2 created.", onLast: true }));
});

gulp.task('task2', function() {
    return gulp.src(['file3.js','file4.js'])
        .pipe(gp_concat('file3_4.js'))
        .pipe(gp_notify({ message: "file3_4 created.", onLast: true }));
});

gulp.task('mainTask', ['task1','task2'], function() {
    gulp.src('file*_*.js')
        .pipe(gp_notify({ message: "All tasks complete.", onLast: true }));
});
Ben
  • 10,056
  • 5
  • 41
  • 42