更多gulp常用插件使用請訪問:gulp常用插件匯總
gulp-notify這是一款gulp通知插件。
安裝
一鍵安裝不多解釋
npm install --save-dev gulp-notify
使用
例1:
var notify = require("gulp-notify");
gulp.src("./src/test.ext")
.pipe(notify("Hello Gulp!"));
例2:
var notify = require("gulp-notify");
gulp.src("./src/test.ext")
.pipe(notify("Found file: <%= file.relative %>!"));
注釋/提示
即使出錯,gulp-notify
也會傳遞vinyl files
。因此,如果您使用的是gulp-plumber
,如果通知程序返回錯誤,則運行不會中斷。
如果你想通知錯誤,可以使用gulp-plumber
不中斷運行,迫使你不得不重新啟動gulp。
您可以使用notify.onError()
作為gulp-plumber
的errorHandler
,如下所示:
gulp.src("../test/fixtures/*")
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(through(function () {
this.emit("error", new Error("Something happend: Error message!"))
}));
API
notify(String)
用於通知流中每個數據的消息。字符串可以是lodash模板,因為它通過gulp-util.template傳遞。notify(Function)
類型:function(VinylFile)
來自gulp
流的Vinyl File
作為參數傳入。
該函數的結果可以是用作消息的字符串或選項對象(請參見下文)。如果返回值是一個字符串,則它可以是lodash模板,因為它是通過gulp-util.template傳遞的。
如果false
從函數返回,則通知將不會運行。notify(options)
選項傳遞到報告程序上,因此在Windows上,您可以定義Growl主機,在Mac上,您可以傳遞contentImage,依此類推。
有關 所有選項,請參閱節點通知程序
默認通知值:
* 定期通知的Gulp徽標
* 錯誤時倒置了Gulp徽標
* 在Mac上,青蛙聲音錯誤。
另請參見高級示例。
options.onLast
類型:Boolean
默認值:false
如果通知僅應在流的最后一個文件上發生。默認情況下,每個文件都會觸發通知。options.emitError
類型:Boolean
默認值:false
返回的流是否應該發出錯誤。如果emitError
為true
,則必須.on('error')
手動處理,以防通知程序(gulp-notify)
失敗。如果false
設置了默認值,則不會發出錯誤,而只是將其打印到控制台。
這意味着您可以在CI系統上運行通知程序,而無需選擇退出,而只是讓其正常地失敗。options.message
類型:String
默認:流中的文件路徑
您希望附加到文件的消息。字符串可以是lodash
模板,因為它通過gulp-util.template傳遞。
范例:Created <%= file.relative %>
。- 作為功能函數
類型:Function(vinylFile)
請參閱notify(Function)
。 options.title
類型:String
默認:“ Gulp通知”
通知的標題。字符串可以是lodash模板,因為它通過gulp-util.template傳遞。
范例:Created <%= file.relative %>
。- 作為功能函數
類型:Function(vinylFile)
請參閱notify(Function)
。 options.templateOptions
類型:Object
默認值:{}
傳遞給lodash
模板的對象,用於傳遞給模板的其他屬性。
gulp.src("../test/fixtures/*")
.pipe(notify({
message: "Generated file: <%= file.relative %> @ <%= options.date %>",
templateOptions: {
date: new Date()
}
}))
options.notifier
類型:Function(options, callback)
默認:node-notifier模塊
通過傳入函數來交換通知程序。該函數需要兩個參數:options
和callback
。
通知完成后必須調用回調。選項將同時包含標題和消息。
請參閱notify.withReporter
語法糖。
notify.on(event, function (notificationOptions)) - Events
如果該wait
選項設置為true
,則通知者將觸發事件click
或timeout
,無論用戶單擊通知還是超時。您在主通知對象(而不是產生流)上偵聽這些事件。
var notify = require('gulp-notify');
notify.on('click', function (options) {
console.log('I clicked something!', options);
});
notify.on('timeout', function (options) {
console.log('The notification timed out', options);
});
gulp.task("click", function () {
return gulp.src("some/glob/**")
.pipe(notify({ message: 'Click or wait', wait: true }));
});
notify.withReporter(Function)
類型: Reporter
包裝options.notifier
僅使用傳入的報告程序返回新的通知功能。
例:
var custom = notify.withReporter(function (options, callback) {
console.log("Title:", options.title);
console.log("Message:", options.message);
callback();
});
gulp.src("../test/fixtures/1.txt")
.pipe(custom("This is a message."));
這將與
gulp.src("../test/fixtures/1.txt")
.pipe(notify({
message: "This is a message."
notifier: function (options, callback) {
console.log("Title:", options.title);
console.log("Message:", options.message);
callback();
}
}));
但是,很多漂亮。
notify.onError()
與using
完全相同的API notify()
,但是在vinyl File
傳遞a
的地方,傳遞錯誤對象。
例:
gulp.src("../test/fixtures/*")
.pipe(through(function () {
this.emit("error", new Error("Something happend: Error message!"))
}))
.on("error", notify.onError(function (error) {
return "Message to the notifier: " + error.message;
}));
或者簡單地:
gulp.src("../test/fixtures/*")
.pipe(through(function () {
this.emit("error", new Error("Something happend: Error message!"))
}))
.on("error", notify.onError("Error: <%= error.message %>"));
gulp.src("../test/fixtures/*")
.pipe(through(function () {
this.emit("error", new Error("Something happend: Error message!"))
}))
.on("error", notify.onError({
message: "Error: <%= error.message %>",
title: "Error running something"
}));
在onError()
終點不支持lodash.template
。
onError()
會自動為您結束視頻流。使觀看更加輕松。
notify.logLevel(level)
類型:Integer
默認值:2
設置是否使用記錄器。如果日志級別設置為0,將不使用任何日志記錄。如果未傳遞新的日志級別,則返回當前日志級別。
- 0:無日志記錄
- 1:登錄錯誤
- 2:記錄錯誤和常規通知。
如果將logging設置為> 0
,則將記錄標題和傳遞給的消息,gulp-notify
如下所示:
➜ gulp-notify git:(master) ✗ gulp --gulpfile examples/gulpfile.js one
[gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
[gulp] Working directory changed to /Users/example/repos/gulp-notify/examples
[gulp] Running 'one'...
[gulp] Finished 'one' in 4.08 ms
[gulp] gulp-notify: [Gulp notification] /Users/example/gulp-notify/test/fixtures/1.txt
禁用 gulp-notify
如果您運行的系統處理通知的能力很差,或者只是不想使用,gulp-notify
而您的項目可以呢?您可以gulp-notify
使用環境變量禁用它DISABLE_NOTIFIER
。
export DISABLE_NOTIFIER=true;
這將禁用所有方法。notify()
,notify.onError
和notify.withReporter
。
例子:
要查看從根目錄運行的所有示例:
$ gulp --gulpfile examples/gulpfile.js --tasks
[gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
[gulp] Working directory changed to /Users/example/gulp-notify/examples
[gulp] Tasks for /Users/example/gulp-notify/examples/gulpfile.js
[gulp] ├── multiple
[gulp] ├── one
[gulp] ├── message
[gulp] ├── customReporter
[gulp] ├── template
[gulp] ├── templateadv
[gulp] ├── function
[gulp] ├── onlast
[gulp] ├── advanceMac
[gulp] ├── error
[gulp] ├── forceGrowl
[gulp] └── customError
運行示例:
$ gulp --gulpfile examples/gulpfile.js multiple
[gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
[gulp] Working directory changed to /Users/example/gulp-notify/examples
[gulp] Running 'multiple'...
[gulp] Finished 'multiple' in 3.75 ms
作為jshint報告員
gulp-notify
可以輕松用作jshint
報告程序。當jshint
在乙烯基文件上公開結果時,我們可以在如下函數中使用它們:
gulp.task('lint', function() {
gulp.src('/src/**/*.js')
.pipe(jshint())
// 使用gulp-notify作為jshint報告器
.pipe(notify(function (file) {
if (file.jshint.success) {
// 不顯示的東西,如果成功
return false;
}
var errors = file.jshint.results.map(function (data) {
if (data.error) {
return "(" + data.error.line + ':' + data.error.character + ') ' + data.error.reason;
}
}).join("\n");
return file.relative + " (" + file.jshint.results.length + " errors)\n" + errors;
}));
});
如果您在中使用消息功能gulp-notify
,則不會顯示該消息。直接使用function
和都是如此{ message: function () {}}
。