1 |
|
2 |
|
3 |
|
4 | var os = require('os'),
|
5 | which = require('which'),
|
6 | utils = require('../lib/utils'),
|
7 | cloneDeep = require('lodash.clonedeep');
|
8 |
|
9 | var EventEmitter = require('events').EventEmitter;
|
10 | var util = require('util');
|
11 |
|
12 | var notifier = 'notify-send', hasNotifier = void 0;
|
13 |
|
14 | module.exports = NotifySend;
|
15 |
|
16 | function NotifySend (options) {
|
17 | options = cloneDeep(options || {});
|
18 | if (!(this instanceof NotifySend)) {
|
19 | return new NotifySend(options);
|
20 | }
|
21 |
|
22 | this.options = options;
|
23 |
|
24 | EventEmitter.call(this);
|
25 | }
|
26 | util.inherits(NotifySend, EventEmitter);
|
27 |
|
28 | NotifySend.prototype.notify = function (options, callback) {
|
29 | options = cloneDeep(options || {});
|
30 | callback = callback || function () {};
|
31 |
|
32 | if (!options.message) {
|
33 | callback(new Error('Message is required.'));
|
34 | return this;
|
35 | }
|
36 |
|
37 | if (os.type() !== 'Linux') {
|
38 | callback(new Error('Only supported on Linux systems'));
|
39 | return this;
|
40 | }
|
41 |
|
42 | if (hasNotifier === false) {
|
43 | callback(new Error('notify-send must be installed on the system.'));
|
44 | return this;
|
45 | }
|
46 |
|
47 | if (hasNotifier || !!this.options.suppressOsdCheck) {
|
48 | doNotification(options, callback);
|
49 | return this;
|
50 | }
|
51 |
|
52 | try {
|
53 | hasNotifier = !!which.sync(notifier);
|
54 | doNotification(options, callback);
|
55 | } catch (err) {
|
56 | hasNotifier = false;
|
57 | return callback(err);
|
58 | };
|
59 |
|
60 | return this;
|
61 | };
|
62 |
|
63 | var allowedArguments = [
|
64 | "urgency",
|
65 | "expire-time",
|
66 | "icon",
|
67 | "category",
|
68 | "hint"
|
69 | ];
|
70 |
|
71 | function doNotification (options, callback) {
|
72 | var initial, argsList;
|
73 |
|
74 | options = utils.mapToNotifySend(options);
|
75 | options.title = options.title || 'Node Notification:';
|
76 |
|
77 | initial = [options.title, options.message];
|
78 | delete options.title;
|
79 | delete options.message;
|
80 |
|
81 | argsList = utils.constructArgumentList(options, {
|
82 | initial: initial,
|
83 | keyExtra: '-',
|
84 | allowedArguments: allowedArguments
|
85 | });
|
86 |
|
87 | utils.command(notifier, argsList, callback);
|
88 | }
|