Search code examples
gulpgulp-notify

How do I process a notification in gulp after all tasks are complete?


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?


Solution

  • 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.