1 |
|
2 |
|
3 |
|
4 | var checkGrowl = require('../lib/checkGrowl');
|
5 | var utils = require('../lib/utils');
|
6 | var growly = require('growly');
|
7 |
|
8 | var EventEmitter = require('events').EventEmitter;
|
9 | var util = require('util');
|
10 |
|
11 | var errorMessageNotFound =
|
12 | "Couldn't connect to growl (might be used as a fallback). Make sure it is running";
|
13 |
|
14 | module.exports = Growl;
|
15 |
|
16 | var hasGrowl;
|
17 |
|
18 | function Growl(options) {
|
19 | options = utils.clone(options || {});
|
20 | if (!(this instanceof Growl)) {
|
21 | return new Growl(options);
|
22 | }
|
23 |
|
24 | growly.appname = options.name || 'Node';
|
25 | this.options = options;
|
26 |
|
27 | EventEmitter.call(this);
|
28 | }
|
29 | util.inherits(Growl, EventEmitter);
|
30 |
|
31 | function notifyRaw(options, callback) {
|
32 | growly.setHost(this.options.host, this.options.port);
|
33 | options = utils.clone(options || {});
|
34 |
|
35 | if (typeof options === 'string') {
|
36 | options = { title: 'node-notifier', message: options };
|
37 | }
|
38 |
|
39 | callback = utils.actionJackerDecorator(this, options, callback, function(
|
40 | data
|
41 | ) {
|
42 | if (data === 'click') {
|
43 | return 'click';
|
44 | }
|
45 | if (data === 'timedout') {
|
46 | return 'timeout';
|
47 | }
|
48 | return false;
|
49 | });
|
50 |
|
51 | options = utils.mapToGrowl(options);
|
52 |
|
53 | if (!options.message) {
|
54 | callback(new Error('Message is required.'));
|
55 | return this;
|
56 | }
|
57 |
|
58 | options.title = options.title || 'Node Notification:';
|
59 |
|
60 | if (hasGrowl || !!options.wait) {
|
61 | var localCallback = options.wait ? callback : noop;
|
62 | growly.notify(options.message, options, localCallback);
|
63 | if (!options.wait) callback();
|
64 | return this;
|
65 | }
|
66 |
|
67 | checkGrowl(growly, function(_, didHaveGrowl) {
|
68 | hasGrowl = didHaveGrowl;
|
69 | if (!didHaveGrowl) return callback(new Error(errorMessageNotFound));
|
70 | growly.notify(options.message, options);
|
71 | callback();
|
72 | });
|
73 | return this;
|
74 | }
|
75 |
|
76 | Object.defineProperty(Growl.prototype, 'notify', {
|
77 | get: function() {
|
78 | if (!this._notify) this._notify = notifyRaw.bind(this);
|
79 | return this._notify;
|
80 | }
|
81 | });
|
82 |
|
83 | function noop() {}
|