Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to using Gulp plugin notify with del?

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"

like image 639
RooksStrife Avatar asked Dec 20 '15 19:12

RooksStrife


2 Answers

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:

  1. clean illustrates how to do it.

  2. fail illustrates the point of being able to handle failures.

  3. 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!'});
});
like image 168
Louis Avatar answered Oct 14 '22 23:10

Louis


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'));
});
like image 43
BrettJephson Avatar answered Oct 14 '22 23:10

BrettJephson