1 | # gulp-notify [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]
|
2 |
|
3 | > notification plugin for [gulp](https://github.com/gulpjs/gulp)
|
4 |
|
5 | ## Information
|
6 | | Package | gulp-notify |
|
7 | | ------------- |--------------|
|
8 | | Description | Send messages to Mac Notification Center, Linux notifications (using `notify-send`) or Windows >= 8 (using native toaster) or Growl as fallback, using the [node-notifier](https://github.com/mikaelbr/node-notifier) module. Can also [specify custom notifier](#notifywithreporterfunction). |
|
9 | | Node Version | >= 0.8 |
|
10 |
|
11 | ## Table of Contents
|
12 |
|
13 |
|
14 |
|
15 | * [Information](#information)
|
16 | * [Requirements](#requirements)
|
17 | * [Usage](#usage)
|
18 | * [Notes/tip](#notestip)
|
19 | * [API](#api)
|
20 | * [notify(String)](#notifystring)
|
21 | * [notify(Function)](#notifyfunction)
|
22 | * [notify(options)](#notifyoptions)
|
23 | * [options.onLast](#optionsonlast)
|
24 | * [options.emitError](#optionsemiterror)
|
25 | * [options.message](#optionsmessage)
|
26 | * [options.title](#optionstitle)
|
27 | * [options.templateOptions](#optionstemplateoptions)
|
28 | * [options.notifier](#optionsnotifier)
|
29 | * [notify.withReporter(Function)](#notifywithreporterfunction)
|
30 | * [notify.onError()](#notifyonerror)
|
31 | * [notify.logLevel(level)](#notifyloglevellevel)
|
32 | * [Disable `gulp-notify`](#disable-gulp-notify)
|
33 | * [Examples](#examples)
|
34 | * [As jshint reporter](#as-jshint-reporter)
|
35 | * [License](#license)
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | ## Requirements
|
42 | * **Mac OS X**: No external installation needed (if Mac OS X 10.8 or higher).
|
43 | * **Linux**: `notify-send`/`notify-osd` should be installed (On Ubuntu this is installed per default)
|
44 | * **Windows**: Uses native toaster (if Windows 8 or higher).
|
45 | * **Fallback: Growl**: Growl (for Mac, Windows or similar) should be installed.
|
46 |
|
47 | See [node-notifier](https://github.com/mikaelbr/node-notifier) for details.
|
48 |
|
49 | **Windows 10 Note:** You might have to activate banner notification for the toast to show.
|
50 |
|
51 | From [#90 (comment)](https://github.com/mikaelbr/gulp-notify/issues/90#issuecomment-129333034)
|
52 | > You can make it work by going to System > Notifications & Actions. The 'toast' app needs to have Banners enabled. (You can activate banners by clicking on the 'toast' app and setting the 'Show notification banners' to On)
|
53 |
|
54 |
|
55 | ## Usage
|
56 |
|
57 | First, install `gulp-notify` as a development dependency:
|
58 |
|
59 | ```shell
|
60 | npm install --save-dev gulp-notify
|
61 | ```
|
62 |
|
63 | Then, add it to your `gulpfile.js`:
|
64 |
|
65 | ```javascript
|
66 |
|
67 | var notify = require("gulp-notify");
|
68 | gulp.src("./src/test.ext")
|
69 | .pipe(notify("Hello Gulp!"));
|
70 | ```
|
71 |
|
72 | Or with template
|
73 |
|
74 |
|
75 | ```javascript
|
76 |
|
77 | var notify = require("gulp-notify");
|
78 | gulp.src("./src/test.ext")
|
79 | .pipe(notify("Found file: <%= file.relative %>!"));
|
80 | ```
|
81 |
|
82 | See [examples](examples/gulpfile.js) for more or the [API](#api) section for various inputs.
|
83 |
|
84 | ## Notes/tip
|
85 |
|
86 | `gulp-notify` passes on the `vinyl files` even on error. So if you are
|
87 | using [`gulp-plumber`](https://github.com/floatdrop/gulp-plumber) the run
|
88 | will not break if the notifier returns an error.
|
89 |
|
90 | If you want to notify on errors [`gulp-plumber`](https://github.com/floatdrop/gulp-plumber)
|
91 | can be used to not break the run and force you to have to restart gulp.
|
92 |
|
93 | You can use [notify.onError()](#notifyonerror) as the errorHandler for gulp-plumber like this:
|
94 |
|
95 | ```javascript
|
96 | gulp.src("../test/fixtures/*")
|
97 | .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
|
98 | .pipe(through(function () {
|
99 | this.emit("error", new Error("Something happend: Error message!"))
|
100 | }));
|
101 | ```
|
102 |
|
103 | ## API
|
104 |
|
105 | ### notify(String)
|
106 |
|
107 | A message to notify per data on stream.
|
108 | The string can be a lodash template as
|
109 | it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
|
110 |
|
111 | ### notify(Function)
|
112 | Type: `function(VinylFile)`
|
113 |
|
114 | Vinyl File from gulp stream passed in as argument.
|
115 |
|
116 | The result of the function can be a string used as the message or an options object (see below).
|
117 | If the returned value is a string, it can be a lodash template as
|
118 | it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
|
119 |
|
120 | If `false` is returned from the function the notification won't run.
|
121 |
|
122 | ### notify(options)
|
123 |
|
124 | *Options are passed onto the reporter, so on Windows, you can define
|
125 | Growl host, on Mac you can pass in contentImage, and so on.
|
126 |
|
127 | See [node-notifier](https://github.com/mikaelbr/node-notifier)
|
128 | for all options*
|
129 |
|
130 | Default notification values:
|
131 | - Gulp logo on regular notification
|
132 | - Inverted Gulp logo on error
|
133 | - Frog sound on error on Mac.
|
134 |
|
135 | See also the [advanced example](examples/gulpfile.js).
|
136 |
|
137 | #### options.onLast
|
138 | Type: `Boolean`
|
139 | Default: `false`
|
140 |
|
141 | If the notification should only happen on the last file
|
142 | of the stream. Per default a notification is triggered
|
143 | on each file.
|
144 |
|
145 | #### options.emitError
|
146 | Type: `Boolean`
|
147 | Default: `false`
|
148 |
|
149 | If the returned stream should emit an error or not.
|
150 | If `emitError` is true, you have to handle `.on('error')`
|
151 | manually in case the notifier (gulp-notify) fails. If
|
152 | the default `false` is set, the error will not be emitted
|
153 | but simply printed to the console.
|
154 |
|
155 | This means you can run the notifier on a CI system without
|
156 | opting it out but simply letting it fail gracefully.
|
157 |
|
158 |
|
159 | #### options.message
|
160 | Type: `String`
|
161 | Default: File path in stream
|
162 |
|
163 | The message you wish to attach to file. The string can be a
|
164 | lodash template as it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
|
165 |
|
166 | Example: `Created <%= file.relative %>`.
|
167 |
|
168 | ##### as function
|
169 | Type: `Function(vinylFile)`
|
170 |
|
171 | See `notify(Function)`.
|
172 |
|
173 | #### options.title
|
174 | Type: `String`
|
175 | Default: "Gulp Notification"
|
176 |
|
177 | The title of the notification. The string can be a
|
178 | lodash template as it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
|
179 |
|
180 | Example: `Created <%= file.relative %>`.
|
181 |
|
182 | ##### as function
|
183 | Type: `Function(vinylFile)`
|
184 |
|
185 | See `notify(Function)`.
|
186 |
|
187 | #### options.templateOptions
|
188 | Type: `Object`
|
189 | Default: {}
|
190 |
|
191 | Object passed to the `lodash` template, for additional properties passed to the template.
|
192 |
|
193 | Examples:
|
194 |
|
195 | ```javascript
|
196 | gulp.src("../test/fixtures/*")
|
197 | .pipe(notify({
|
198 | message: "Generated file: <%= file.relative %> @ <%= options.date %>",
|
199 | templateOptions: {
|
200 | date: new Date()
|
201 | }
|
202 | }))
|
203 | ```
|
204 |
|
205 | #### options.notifier
|
206 | Type: `Function(options, callback)`
|
207 | Default: node-notifier module
|
208 |
|
209 | Swap out the notifier by passing in an function.
|
210 | The function expects two arguments: options and callback.
|
211 |
|
212 | The callback must be called when the notification is finished. Options
|
213 | will contain both title and message.
|
214 |
|
215 | *See `notify.withReporter` for syntactic sugar.*
|
216 |
|
217 |
|
218 | ### notify.on(event, function (notificationOptions)) - Events
|
219 |
|
220 | **If the `wait` option is set to `true`**, the notifier will trigger
|
221 | events `click` or `timeout`, whether the user clicks the notification or it
|
222 | times out. You listen to these events on the main notify object, not the
|
223 | produces stream.
|
224 |
|
225 | ```js
|
226 | var notify = require('gulp-notify');
|
227 |
|
228 | notify.on('click', function (options) {
|
229 | console.log('I clicked something!', options);
|
230 | });
|
231 |
|
232 | notify.on('timeout', function (options) {
|
233 | console.log('The notification timed out', options);
|
234 | });
|
235 |
|
236 | gulp.task("click", function () {
|
237 | return gulp.src("some/glob/**")
|
238 | .pipe(notify({ message: 'Click or wait', wait: true }));
|
239 | });
|
240 | ```
|
241 |
|
242 | ### notify.withReporter(Function)
|
243 | Type: `Reporter`
|
244 |
|
245 | Wraps `options.notifier` to return a new notify-function only using
|
246 | the passed in reporter.
|
247 |
|
248 | Example:
|
249 |
|
250 | ```javascript
|
251 | var custom = notify.withReporter(function (options, callback) {
|
252 | console.log("Title:", options.title);
|
253 | console.log("Message:", options.message);
|
254 | callback();
|
255 | });
|
256 |
|
257 | gulp.src("../test/fixtures/1.txt")
|
258 | .pipe(custom("This is a message."));
|
259 |
|
260 | ```
|
261 |
|
262 | This will be the same as
|
263 |
|
264 | ```javascript
|
265 |
|
266 | gulp.src("../test/fixtures/1.txt")
|
267 | .pipe(notify({
|
268 | message: "This is a message."
|
269 | notifier: function (options, callback) {
|
270 | console.log("Title:", options.title);
|
271 | console.log("Message:", options.message);
|
272 | callback();
|
273 | }
|
274 | }));
|
275 | ```
|
276 |
|
277 | But much, much prettier.
|
278 |
|
279 |
|
280 | ### notify.onError()
|
281 |
|
282 | The exact same API as using `notify()`, but where a `vinyl File`
|
283 | is passed, the error object is passed instead.
|
284 |
|
285 | Example:
|
286 |
|
287 | ```javascript
|
288 | gulp.src("../test/fixtures/*")
|
289 | .pipe(through(function () {
|
290 | this.emit("error", new Error("Something happend: Error message!"))
|
291 | }))
|
292 | .on("error", notify.onError(function (error) {
|
293 | return "Message to the notifier: " + error.message;
|
294 | }));
|
295 | ```
|
296 |
|
297 | Or simply:
|
298 |
|
299 | ```javascript
|
300 | gulp.src("../test/fixtures/*")
|
301 | .pipe(through(function () {
|
302 | this.emit("error", new Error("Something happend: Error message!"))
|
303 | }))
|
304 | .on("error", notify.onError("Error: <%= error.message %>"));
|
305 | ```
|
306 |
|
307 | ```javascript
|
308 | gulp.src("../test/fixtures/*")
|
309 | .pipe(through(function () {
|
310 | this.emit("error", new Error("Something happend: Error message!"))
|
311 | }))
|
312 | .on("error", notify.onError({
|
313 | message: "Error: <%= error.message %>",
|
314 | title: "Error running something"
|
315 | }));
|
316 | ```
|
317 |
|
318 | The `onError()` end point does support `lodash.template`.
|
319 |
|
320 | **`onError()` will automatically end the stream for you. Making it easer for watching.**
|
321 |
|
322 | ### notify.logLevel(level)
|
323 | Type: `Integer`
|
324 | Default: `2`
|
325 |
|
326 | Set if logger should be used or not. If log level is set to 0,
|
327 | no logging will be used. If no new log level is passed, the
|
328 | current log level is returned.
|
329 |
|
330 | * `0`: No logging
|
331 | * `1`: Log on error
|
332 | * `2`: Log both on error and regular notification.
|
333 |
|
334 | If logging is set to `> 0`, the title and
|
335 | message passed to `gulp-notify` will be logged like so:
|
336 |
|
337 | ```sh
|
338 | ➜ gulp-notify git:(master) ✗ gulp --gulpfile examples/gulpfile.js one
|
339 | [gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
|
340 | [gulp] Working directory changed to /Users/example/repos/gulp-notify/examples
|
341 | [gulp] Running 'one'...
|
342 | [gulp] Finished 'one' in 4.08 ms
|
343 | [gulp] gulp-notify: [Gulp notification] /Users/example/gulp-notify/test/fixtures/1.txt
|
344 | ```
|
345 |
|
346 | ## Disable `gulp-notify`
|
347 |
|
348 | If you are running on a system that handles notifications poorly or you simply
|
349 | do not wish to use `gulp-notify` but your project does? You can disable `gulp-notify`
|
350 | by using enviroment variable `DISABLE_NOTIFIER`.
|
351 |
|
352 | ```
|
353 | export DISABLE_NOTIFIER=true;
|
354 | ```
|
355 |
|
356 | This will disable all methods; `notify()`, `notify.onError` and `notify.withReporter`.
|
357 |
|
358 | ## Examples
|
359 |
|
360 | To see all examples run from root:
|
361 |
|
362 | ```shell
|
363 | $ gulp --gulpfile examples/gulpfile.js --tasks
|
364 | [gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
|
365 | [gulp] Working directory changed to /Users/example/gulp-notify/examples
|
366 | [gulp] Tasks for /Users/example/gulp-notify/examples/gulpfile.js
|
367 | [gulp] ├── multiple
|
368 | [gulp] ├── one
|
369 | [gulp] ├── message
|
370 | [gulp] ├── customReporter
|
371 | [gulp] ├── template
|
372 | [gulp] ├── templateadv
|
373 | [gulp] ├── function
|
374 | [gulp] ├── onlast
|
375 | [gulp] ├── advanceMac
|
376 | [gulp] ├── error
|
377 | [gulp] ├── forceGrowl
|
378 | [gulp] └── customError
|
379 | ```
|
380 |
|
381 | To run an example:
|
382 | ```shell
|
383 | $ gulp --gulpfile examples/gulpfile.js multiple
|
384 | [gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
|
385 | [gulp] Working directory changed to /Users/example/gulp-notify/examples
|
386 | [gulp] Running 'multiple'...
|
387 | [gulp] Finished 'multiple' in 3.75 ms
|
388 | ```
|
389 |
|
390 | ### As jshint reporter
|
391 |
|
392 | `gulp-notify` can easily be used as jshint reporter.
|
393 | As jshint exposes the result on the vinyl file we can
|
394 | use them in a function like so:
|
395 |
|
396 | ```javascript
|
397 | gulp.task('lint', function() {
|
398 | gulp.src('/src/**/*.js')
|
399 | .pipe(jshint())
|
400 | // Use gulp-notify as jshint reporter
|
401 | .pipe(notify(function (file) {
|
402 | if (file.jshint.success) {
|
403 | // Don't show something if success
|
404 | return false;
|
405 | }
|
406 |
|
407 | var errors = file.jshint.results.map(function (data) {
|
408 | if (data.error) {
|
409 | return "(" + data.error.line + ':' + data.error.character + ') ' + data.error.reason;
|
410 | }
|
411 | }).join("\n");
|
412 | return file.relative + " (" + file.jshint.results.length + " errors)\n" + errors;
|
413 | }));
|
414 | });
|
415 | ```
|
416 |
|
417 | If you use a function for message in `gulp-notify`, the message won't be shown.
|
418 | This is true for both direct use of function and `{ message: function () {}}`.
|
419 |
|
420 | [![NPM downloads][npm-downloads]][npm-url]
|
421 |
|
422 | ## License
|
423 |
|
424 | [MIT License](http://en.wikipedia.org/wiki/MIT_License)
|
425 |
|
426 | [npm-url]: https://npmjs.org/package/gulp-notify
|
427 | [npm-image]: http://img.shields.io/npm/v/gulp-notify.svg?style=flat
|
428 | [npm-downloads]: http://img.shields.io/npm/dm/gulp-notify.svg?style=flat
|
429 |
|
430 | [travis-url]: http://travis-ci.org/mikaelbr/gulp-notify
|
431 | [travis-image]: http://img.shields.io/travis/mikaelbr/gulp-notify.svg?style=flat
|
432 |
|
433 | [depstat-url]: https://gemnasium.com/mikaelbr/gulp-notify
|
434 | [depstat-image]: http://img.shields.io/gemnasium/mikaelbr/gulp-notify.svg?style=flat
|