UNPKG

1.82 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 = "Couldn't connect to growl (might be used as a fallback). Make sure it is running";
12
13module.exports = Growl;
14
15var hasGrowl = void 0;
16
17function 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}
28util.inherits(Growl, EventEmitter);
29
30Growl.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
75function noop() {
76}