UNPKG

1.96 kBJavaScriptView Raw
1/**
2 * Wrapper for the growly module
3 */
4var checkGrowl = require('../lib/checkGrowl');
5var utils = require('../lib/utils');
6var growly = require('growly');
7
8var EventEmitter = require('events').EventEmitter;
9var util = require('util');
10
11var errorMessageNotFound =
12 "Couldn't connect to growl (might be used as a fallback). Make sure it is running";
13
14module.exports = Growl;
15
16var hasGrowl;
17
18function 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}
29util.inherits(Growl, EventEmitter);
30
31function 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
76Object.defineProperty(Growl.prototype, 'notify', {
77 get: function() {
78 if (!this._notify) this._notify = notifyRaw.bind(this);
79 return this._notify;
80 }
81});
82
83function noop() {}