UNPKG

3.63 kBJavaScriptView Raw
1var cp = require('child_process')
2 , os = require('os')
3 , fs = require('fs')
4 , osVersionError = 'Incorrect OS. node-notify requires Mac OS 10.8 or higher'
5 , shellwords = require('shellwords')
6 ;
7
8var escapeQuotes = function (str) {
9 if (typeof str === 'string') {
10 return str.replace(/(["$`\\])/g, '\\$1');
11 } else {
12 return str;
13 }
14};
15
16var inArray = function (arr, val) {
17 for(var i = 0; i < arr.length; i++) {
18 if (arr[i] === val) {
19 return true;
20 }
21 }
22 return false;
23};
24
25var notifySendFlags = {
26 "u": "urgency",
27 "urgency": "urgency",
28 "t": "expire-time",
29 "e": "expire-time",
30 "expire": "expire-time",
31 "expire-time": "expire-time",
32 "i": "icon",
33 "icon": "icon",
34 "c": "category",
35 "category": "category",
36 "h": "hint",
37 "hint": "hint"
38};
39
40module.exports.command = function (notifier, options, cb{
41 var notifyApp = cp.exec(shellwords.escape(notifier) + ' ' + options.join(' '), function (error, stdout, stderr) {
42 if (error) {
43 return cb(error);
44 }
45
46 cb(stderr, stdout);
47 });
48
49 return notifyApp;
50};
51
52var mapAppIcon = function (options) {
53 if (options.appIcon) {
54 options.icon = options.appIcon;
55 delete options.appIcon;
56 }
57
58 return options;
59};
60
61var mapText = function (options) {
62 if (options.text) {
63 options.message = options.text;
64 delete options.text;
65 }
66
67 return options;
68};
69
70var mapIconShorthand = function (options) {
71 if (options.i) {
72 options.icon = options.i;
73 delete options.i;
74 }
75
76 return options;
77};
78
79module.exports.mapToNotifySend = function (options) {
80 options = mapAppIcon(options);
81 options = mapText(options);
82
83 for (var key in options) {
84 if (key === "message" || key === "title") continue;
85 if (options.hasOwnProperty(key) && (notifySendFlags[key] != key)) {
86 options[notifySendFlags[key]] = options[key];
87 delete options[key];
88 }
89 }
90
91 return options;
92};
93
94module.exports.mapToGrowl = function (options) {
95 options = mapAppIcon(options);
96 options = mapIconShorthand(options);
97
98 if (options.message) {
99 options.text = options.message;
100 delete options.message;
101 }
102
103 if (options.icon && !Buffer.isBuffer(options.icon)) {
104 options.icon = fs.readFileSync(options.icon);
105 }
106
107 return options;
108};
109
110module.exports.mapToMac = function (options) {
111 options = mapIconShorthand(options);
112 options = mapText(options);
113
114 if (options.icon) {
115 options.appIcon = options.icon;
116 delete options.icon;
117 }
118
119 return options;
120};
121
122module.exports.constructArgumentList = function (options, initial, keyExtra, allowedArguments) {
123 var args = [];
124 keyExtra = keyExtra || "";
125 var checkForAllowed = allowedArguments !== void 0;
126
127 (initial || []).forEach(function (val{
128 args.push('"' + escapeQuotes(val) + '"');
129 });
130 for(var key in options) {
131 if (options.hasOwnProperty(key) && (!checkForAllowed || inArray(allowedArguments, key))) {
132 args.push('-' + keyExtra + key, '"' + escapeQuotes(options[key]) + '"');
133 }
134 }
135 return args;
136};
137
138module.exports.getOSXVersion = function (cb) {
139 return cp.exec("sw_vers -productVersion", cb);
140}
141
142module.exports.isMacOSX = function (cb) {
143 if (os.type().toLowerCase() != 'darwin') {
144 return cb(true, "You can't use the terminal-notifier reporter unless you are on a Mac.");
145 }
146 return module.exports.getOSXVersion(function (error, stdout, stderr) {
147 if (error) {
148 return cb(true, error, stderr);
149 }
150 if (Number(stdout.split(".")[1]) >= 8) {
151 return cb(false);
152 }
153
154 return cb(true, osVersionError);
155 });
156};