简体   繁体   English

如何使用Gulp插件使用del进行通知?

[英]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 德尔= https://www.npmjs.com/package/del

Notify = https://www.npmjs.com/package/gulp-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. 这将在重建distFolder中的所有内容之前将其清除。

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" 上面返回错误-“ TypeError:del(...)。pipe不是一个函数”

The key to getting this done right is that del returns a promise. 正确完成此任务的关键是del返回一个诺言。 So you have to handle the promise. 因此,您必须兑现承诺。

I've created a gulpfile that has 3 tasks: 我创建了一个具有3个任务的gulpfile:

  1. clean illustrates how to do it. clean演示了如何做。

  2. fail illustrates the point of being able to handle failures. fail说明了能够处理失败的要点。

  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. incorrect的方法会在OP的自我解答中复制该方法。这是不正确的,因为del无论成功与否都会返回一个promise对象。 So the && test will always evaluate the 2nd part of the expression and thus will always notify Clean Done! 因此, &&测试将始终对表达式的第二部分求值,因此将始终通知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). 如果查看Del模块,它不会返回流,因此将没有管道功能(如错误说明所示)。

I'd probably use gulp-clean because it better integrates with gulp's streaming. 我可能会使用gulp-clean,因为它可以更好地与gulp的流媒体集成。

eg 例如

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'));
});

Solved it - 解决了-

Node's notifier is a dependency of notify. 节点的通知程序是notify的依赖项。 So it should be in node_modules already. 因此它应该已经在node_modules中。 Depending on your NPM version it might not be in the root. 根据您的NPM版本,它可能不在根目录中。

Add without NPM install - var notifier = require('node-notifier'); 无需NPM安装即可添加-var var notifier = require('node-notifier');

gulp.task('clean', function(){
  return del(dist) && notifier.notify({message:'Clean Done!'})
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM