More gulp common plug-ins use please visit: gulp common plug-ins summary
gulp-notify This is a gulp notification plug.
Greater use of the document, please click visit gulp-notify tool official website .
installation
A key installation much explanation
npm install --save-dev gulp-notify
use
Example 1:
var notify = require("gulp-notify");
gulp.src("./src/test.ext")
.pipe(notify("Hello Gulp!"));
Example 2:
var notify = require("gulp-notify");
gulp.src("./src/test.ext")
.pipe(notify("Found file: <%= file.relative %>!"));
For more input, see examples , or see the API section.
Notes / tips
even if wrong, gulp-notify
will pass vinyl files
. So, if you are using gulp-plumber
, if the notification procedure returns an error, run without interruption.
If you want to notice the error, you can use gulp-plumber
without interrupting operation, force you to restart gulp.
You can use notify.onError()
as gulp-plumber
is errorHandler
, as follows:
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)
Message for notifying of each data stream. The string can be lodash template as it passes through gulp-util.template pass.notify(Function)
Type:function(VinylFile)
from thegulp
streamVinyl File
as a parameter.
The result of this function can be used as a string or object message option (see below). If the return value is a string, it may be lodash template, as it was by gulp-util.template transfer.
If youfalse
return from the function, the notification will not run.notify(options)
Options transferred to the reporting process, so in Windows, you can define Growl host, on a Mac, you can pass contentImage, and so on.
Of all options, please refer to the node notification procedures- Gulp regularly informed of the logo
- Error logo upside down Gulp
- On a Mac, frog sounds wrong.
See advanced examples .
options.onLast
Type:Boolean
Default:false
If the notification should occur only on the last file stream. By default, each file will trigger a notification.
options.emitError
Type:Boolean
Default:false
Returns whether the flow should issue an error. IfemitError
istrue
, it must be.on('error')
handled manually in case notification procedures(gulp-notify)
fail. If youfalse
set a default value, it will not send the wrong, but simply print it to the console.
This means that you can run the program on CI notification system without opt-out, but only let normally failure.options.message
Type:String
Default: file path in the stream
that you want to attach a file to the message. The string can belodash
a template, because it is by gulp-util.template pass.
Example:Created <%= file.relative %>
.- As a performance function
type:Function(vinylFile)
Seenotify(Function)
. options.title
Type:String
Default: "Gulp notice"
heading notice. The string can be lodash template as it passes through gulp-util.template pass.
Example:Created <%= file.relative %>
.- As a performance function
type:Function(vinylFile)
Seenotify(Function)
. options.templateOptions
Type:Object
Default: {}
is transmitted to thelodash
object template, for delivery to another attribute template.
gulp.src("../test/fixtures/*")
.pipe(notify({
message: "Generated file: <%= file.relative %> @ <%= options.date %>",
templateOptions: {
date: new Date()
}
}))
options.notifier
Type:Function(options, callback)
Default: node-notifier module
exchange function by passing notification process. This function takes two parameters:options
andcallback
.
We must call a callback after notification of completion. Options will also include a title and message.
Seenotify.withReporter
syntax sugar.
notify.on (event, function (notificationOptions)
) - Events If this wait
option is set true
, the notification will trigger event click
or timeout
, whether the user clicks the notification or timeout. You listen for these events on the main notice objects (rather than produce a stream).
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)
Type: Reporter
Packing options.notifier
only passed in the reporting procedure returns the new notification feature.
Example:
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."));
This will work with
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();
}
}));
However, a lot of beautiful.
notify.onError()
And using
exactly the same API notify()
, but the vinyl File
delivery a
place, it sends the wrong object.
Example:
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;
}));
Or simply:
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"
}));
In onError()
does not support end lodash.template
.
onError()
For you automatically end the video stream. It makes viewing easier.
notify.logLevel (level)
Type: Integer
Default: 2
setting whether to use the recorder. If the log level is set to 0, you will not use any logging. If the new log level is passed, the current log level is returned.
- 0: No logging
- 1: Error Log
- 2: Record errors and routine notification.
If logging is set> 0
, then the record is transmitted to the message header and,gulp-notify
as shown below:
➜ 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
Disable gulp-notify
a poor ability to deal with if you are running notifications, or just do not want to use, gulp-notify
and you can do the project? You can gulp-notify
use environment variables to disable it DISABLE_NOTIFIER
.
export DISABLE_NOTIFIER=true;
This will disable all methods. notify()
, notify.onError
And notify.withReporter
.
Example:
To see all of the examples run from the root directory:
$ 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
Run the sample:
$ 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
As jshint Rapporteur
gulp-notify
It can easily be used as jshint
reporting procedures. When jshint
disclosing the results on the vinyl file, we can use them in the following functions:
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;
}));
});
If you use the messaging feature in gulp-notify
, the message will not be displayed. Directly function
and true { message: function () {}}
.