How do I tell Gulp when a task is complete?
I am trying to write a gulp plugin that will count the number of files in a stream. I used this as a starting point :
function count() {
var count = 0;
function countFiles(data) {
count++;
// added this as per official docs:
this.queue(data);
}
function endStream() {
console.log(count + " files processed");
// not doing this as per original post, as it "ends" the gulp chain:
//this.emit("end");
// so doing this instead as per official docs:
this.queue(null);
}
return through(countFiles, endStream);
}
module.exports = count;
And here's an example task:
gulp.task("mytask", function () {
gulp
.src("...files...")
.pipe(count()); // <--- here it is
.pipe(changed("./some/path"))
.pipe(uglify())
.pipe(rename({ extname: ".min.js" }))
.pipe(gulp.dest(./some/path))
.pipe(count()); // <--- here it is again
});
It works great except it doesn't start / end as expected:
[14:39:12] Using gulpfile c:\foo\bar\baz\gulpfile.js
[14:39:12] Starting 'mytask'...
[14:39:12] Finished 'mytask' after 9.74 ms
9 files processed
5 files processed
This means that the thing is executed asynchronously and ends when the task completes. The docs say you should use a callback or return a stream. This seems to be how it is done.
How do I make this function behave? Is this what I am using the plugin through
and not through2
?
+1
source to share