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.
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?
To indicate to gulp that an error occurred in a task using an error-first callback, call it with an Error as the only argument. However, you'll often pass this callback to another API instead of calling it yourself.
Gulp is a task runner that uses Node. js as a platform. It purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. Gulp builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.
To have your tasks execute in order, use the series() method. For tasks to run at maximum concurrency, combine them with the parallel() method. Tasks are composed immediately when either series() or parallel() is called. This allows variation in the composition instead of conditional behavior inside individual tasks.
The Watch method is used to monitor your source files. When any changes to the source file is made, the watch will run an appropriate task. You can use the 'default' task to watch for changes to HTML, CSS, and JavaScript files.
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.
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 }));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With