2015-12-20 11 views
6

これは簡単です...私はdelが完了したという通知を作成しようとしています。Gulpプラグインを使用してdelで通知する方法は?

デル=

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

私が持っている:

それが再構築される前にdistFolderのすべてをクリアし
gulp.task('clean', function() { 
    return del(['distFolder']); 
}); 

。あなたの場合:「デル(...)パイプが関数ではありません。TypeError例外」

+0

あなたが持っているものをしようとするとどうなりますか? –

+0

@ColinMarshall - TypeError:del(...)。pipeは関数ではありません – RooksStrife

答えて

3

これが正しく行わ取得するための鍵は、delは約束を返すことです。あなたは約束を守らなければなりません。

私は3つのタスクがあるgulpfile作成しました:

  1. cleanはそれを行う方法を示します。

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

  3. OP's self-answerのメソッドを複製します。delは成功したかどうかにかかわらず約束オブジェクトを返すため、間違っています。したがって、&&テストでは、式の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!'}); 
}); 
1

-

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

上記のエラーを返します:

私は何をしようとしていることは、以下のようなものですDelモジュールがストリームを返さないので、パイプ関数は存在しません(エラーが説明しているように)。

おそらく、gulpのストリーミングと統合されているため、gulp-cleanを使用しています。

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

はそれを解決 -

ノードの通知は、通知の依存関係です。したがって、すでにnode_modules内にあるはずです。 NPMのバージョンによっては、ルートにない可能性があります。

がインストールNPMなし追加 - var notifier = require('node-notifier');

gulp.task('clean', function(){ 
    return del(dist) && notifier.notify({message:'Clean Done!'}) 
}); 
関連する問題