これを正しく行うための鍵del
は、promise を返すことです。そのため、約束を処理する必要があります。
3 つのタスクを持つ gulpfile を作成しました。
clean
にその方法を示します。
fail
は、障害を処理できる点を示しています。
incorrect
OPの自己回答でメソッドを複製し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!'});
});