UNPKG

12.8 kBMarkdownView Raw
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<!-- toc -->
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<!-- toc stop -->
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
47See [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
51From [#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
57First, install `gulp-notify` as a development dependency:
58
59```shell
60npm install --save-dev gulp-notify
61```
62
63Then, add it to your `gulpfile.js`:
64
65```javascript
66
67var notify = require("gulp-notify");
68gulp.src("./src/test.ext")
69 .pipe(notify("Hello Gulp!"));
70```
71
72Or with template
73
74
75```javascript
76
77var notify = require("gulp-notify");
78gulp.src("./src/test.ext")
79 .pipe(notify("Found file: <%= file.relative %>!"));
80```
81
82See [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
87using [`gulp-plumber`](https://github.com/floatdrop/gulp-plumber) the run
88will not break if the notifier returns an error.
89
90If you want to notify on errors [`gulp-plumber`](https://github.com/floatdrop/gulp-plumber)
91can be used to not break the run and force you to have to restart gulp.
92
93You can use [notify.onError()](#notifyonerror) as the errorHandler for gulp-plumber like this:
94
95```javascript
96gulp.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
107A message to notify per data on stream.
108The string can be a lodash template as
109it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
110
111### notify(Function)
112Type: `function(VinylFile)`
113
114Vinyl File from gulp stream passed in as argument.
115
116The result of the function can be a string used as the message or an options object (see below).
117If the returned value is a string, it can be a lodash template as
118it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
119
120If `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
125Growl host, on Mac you can pass in contentImage, and so on.
126
127See [node-notifier](https://github.com/mikaelbr/node-notifier)
128for all options*
129
130Default notification values:
131 - Gulp logo on regular notification
132 - Inverted Gulp logo on error
133 - Frog sound on error on Mac.
134
135See also the [advanced example](examples/gulpfile.js).
136
137#### options.onLast
138Type: `Boolean`
139Default: `false`
140
141If the notification should only happen on the last file
142of the stream. Per default a notification is triggered
143on each file.
144
145#### options.emitError
146Type: `Boolean`
147Default: `false`
148
149If the returned stream should emit an error or not.
150If `emitError` is true, you have to handle `.on('error')`
151manually in case the notifier (gulp-notify) fails. If
152the default `false` is set, the error will not be emitted
153but simply printed to the console.
154
155This means you can run the notifier on a CI system without
156opting it out but simply letting it fail gracefully.
157
158
159#### options.message
160Type: `String`
161Default: File path in stream
162
163The message you wish to attach to file. The string can be a
164lodash template as it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
165
166Example: `Created <%= file.relative %>`.
167
168##### as function
169Type: `Function(vinylFile)`
170
171See `notify(Function)`.
172
173#### options.title
174Type: `String`
175Default: "Gulp Notification"
176
177The title of the notification. The string can be a
178lodash template as it is passed through [gulp-util.template](https://github.com/gulpjs/gulp-util#templatestring-data).
179
180Example: `Created <%= file.relative %>`.
181
182##### as function
183Type: `Function(vinylFile)`
184
185See `notify(Function)`.
186
187#### options.templateOptions
188Type: `Object`
189Default: {}
190
191Object passed to the `lodash` template, for additional properties passed to the template.
192
193Examples:
194
195```javascript
196gulp.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
206Type: `Function(options, callback)`
207Default: node-notifier module
208
209Swap out the notifier by passing in an function.
210The function expects two arguments: options and callback.
211
212The callback must be called when the notification is finished. Options
213will 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
221events `click` or `timeout`, whether the user clicks the notification or it
222times out. You listen to these events on the main notify object, not the
223produces stream.
224
225```js
226var notify = require('gulp-notify');
227
228notify.on('click', function (options) {
229 console.log('I clicked something!', options);
230});
231
232notify.on('timeout', function (options) {
233 console.log('The notification timed out', options);
234});
235
236gulp.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)
243Type: `Reporter`
244
245Wraps `options.notifier` to return a new notify-function only using
246the passed in reporter.
247
248Example:
249
250```javascript
251var custom = notify.withReporter(function (options, callback) {
252 console.log("Title:", options.title);
253 console.log("Message:", options.message);
254 callback();
255});
256
257gulp.src("../test/fixtures/1.txt")
258 .pipe(custom("This is a message."));
259
260```
261
262This will be the same as
263
264```javascript
265
266gulp.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
277But much, much prettier.
278
279
280### notify.onError()
281
282The exact same API as using `notify()`, but where a `vinyl File`
283is passed, the error object is passed instead.
284
285Example:
286
287```javascript
288gulp.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
297Or simply:
298
299```javascript
300gulp.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
308gulp.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
318The `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)
323Type: `Integer`
324Default: `2`
325
326Set if logger should be used or not. If log level is set to 0,
327no logging will be used. If no new log level is passed, the
328current log level is returned.
329
330* `0`: No logging
331* `1`: Log on error
332* `2`: Log both on error and regular notification.
333
334If logging is set to `> 0`, the title and
335message 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
348If you are running on a system that handles notifications poorly or you simply
349do not wish to use `gulp-notify` but your project does? You can disable `gulp-notify`
350by using enviroment variable `DISABLE_NOTIFIER`.
351
352```
353export DISABLE_NOTIFIER=true;
354```
355
356This will disable all methods; `notify()`, `notify.onError` and `notify.withReporter`.
357
358## Examples
359
360To 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
381To 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.
393As jshint exposes the result on the vinyl file we can
394use them in a function like so:
395
396```javascript
397gulp.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
417If you use a function for message in `gulp-notify`, the message won't be shown.
418This 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