如何使用Gulp插件通知del?

Roo*_*ife 6 javascript gulp gulp-notify

这应该很容易......我正在尝试创建del完成的通知.

Del = https://www.npmjs.com/package/del

Notify = https://www.npmjs.com/package/gulp-notify

我有:

gulp.task('clean', function() {
    return del(['distFolder']);
});
Run Code Online (Sandbox Code Playgroud)

这会在重建之前清除distFolder中的所有内容.

我想要做的是如下:

gulp.task('clean', function() {
    return del(['distFolder']).pipe(notify('Clean task finished'));
});
Run Code Online (Sandbox Code Playgroud)

以上返回错误 - "TypeError:del(...).pipe不是函数"

Lou*_*uis 3

正确完成这件事的关键是del返回一个承诺。所以你必须履行承诺。

我创建了一个 gulpfile,它有 3 个任务:

  1. clean说明了如何做。

  2. fail说明了能够处理故障的要点。

  3. incorrect复制OP自我回答中的方法这是不正确的,因为del无论是否成功都会返回一个promise对象。因此,&&测试将始终评估表达式的第二部分,因此Clean Done!即使出现错误并且没有删除任何内容,也始终会发出通知。

这是代码:

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!'});
});
Run Code Online (Sandbox Code Playgroud)