6

これは簡単なはずです...デルが完了したという通知を作成しようとしています。

デル= https://www.npmjs.com/package/del

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

私は持っている:

gulp.task('clean', function() {
    return del(['distFolder']);
});

これにより、再構築される前に distFolder 内のすべてがクリアされます。

私がやろうとしていることは、以下のようなものです:

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

上記はエラーを返します - 「TypeError: del(...).pipe is not a function」

4

3 に答える 3

3

これを正しく行うための鍵delは、promise を返すことです。そのため、約束を処理する必要があります。

3 つのタスクを持つ gulpfile を作成しました。

  1. cleanにその方法を示します。

  2. failは、障害を処理できる点を示しています。

  3. incorrectOPの自己回答でメソッドを複製しdelます成功したかどうかに関係なくpromiseオブジェクトを返すため、正しくありません。したがって、&&テストは常に式の 2 番目の部分を評価するため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!'});
});
于 2016-01-28T00:23:38.377 に答える