This should be easy... I am trying to create a notification that the del is done.
Del = https://www.npmjs.com/package/del
Notify = https://www.npmjs.com/package/gulp-notify
I have:
gulp.task('clean', function() {
return del(['distFolder']);
});
That clears everything in the distFolder before it gets rebuilt.
What I am trying to do is something like below:
gulp.task('clean', function() {
return del(['distFolder']).pipe(notify('Clean task finished'));
});
The above returns an error - "TypeError: del(...).pipe is not a function"
The key to getting this done right is that del
returns a promise. So you have to handle the promise.
I've created a gulpfile that has 3 tasks:
clean
illustrates how to do it.
fail
illustrates the point of being able to handle failures.
incorrect
replicates the method in the OP's self-answer It is incorrect because del
returns a promise object whether or not it is successful. So the &&
test will always evaluate the 2nd part of the expression and thus will always notify Clean Done!
even if there was an error and nothing was deleted.
Here's the code:
var gulp = require("gulp");
var notifier = require("node-notifier");
var del = require("del");
// This is how you should do it.
gulp.task('clean', function(){
return del("build").then(function () {
notifier.notify({message:'Clean Done!'});
}).catch(function () {
notifier.notify({message:'Clean Failed!'});
});
});
//
// Illustrates a failure to delete. You should first do:
//
// 1. mkdir protected
// 2. touch protected/foo.js
// 3. chmod a-rwx protected
//
gulp.task('fail', function(){
return del("protected/**").then (function () {
notifier.notify({message:'Clean Done!'});
}).catch(function () {
notifier.notify({message:'Clean Failed!'});
});
});
// Contrary to what the OP has in the self-answer, this is not the
// correct way to do it. See the previous task for how you must setup
// your FS to get an error. This will fail to delete anything but
// you'll still get the "Clean Done" message.
gulp.task('incorrect', function(){
return del("protected/**") && notifier.notify({message:'Clean Done!'});
});
If you look at the Del module it isn't returning a stream, so there will be no pipe function (as the error explains).
I'd probably use gulp-clean because it better integrates with gulp's streaming.
e.g.
var clean = require('gulp-clean');
var notify = require('gulp-notify');
gulp.task('clean', function() {
return gulp.src('distFolder', {read: false})
.pipe(clean())
.pipe(notify('Clean task finished'));
});
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