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