utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. var shellwords = require('shellwords');
  2. var cp = require('child_process');
  3. var semver = require('semver');
  4. var isWSL = require('is-wsl');
  5. var path = require('path');
  6. var url = require('url');
  7. var os = require('os');
  8. var fs = require('fs');
  9. var net = require('net');
  10. const BUFFER_SIZE = 1024;
  11. function clone(obj) {
  12. return JSON.parse(JSON.stringify(obj));
  13. }
  14. module.exports.clone = clone;
  15. var escapeQuotes = function (str) {
  16. if (typeof str === 'string') {
  17. return str.replace(/(["$`\\])/g, '\\$1');
  18. } else {
  19. return str;
  20. }
  21. };
  22. var inArray = function (arr, val) {
  23. return arr.indexOf(val) !== -1;
  24. };
  25. var notifySendFlags = {
  26. u: 'urgency',
  27. urgency: 'urgency',
  28. t: 'expire-time',
  29. time: 'expire-time',
  30. timeout: 'expire-time',
  31. e: 'expire-time',
  32. expire: 'expire-time',
  33. 'expire-time': 'expire-time',
  34. i: 'icon',
  35. icon: 'icon',
  36. c: 'category',
  37. category: 'category',
  38. subtitle: 'category',
  39. h: 'hint',
  40. hint: 'hint',
  41. a: 'app-name',
  42. 'app-name': 'app-name'
  43. };
  44. module.exports.command = function (notifier, options, cb) {
  45. notifier = shellwords.escape(notifier);
  46. if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) {
  47. console.info('node-notifier debug info (command):');
  48. console.info('[notifier path]', notifier);
  49. console.info('[notifier options]', options.join(' '));
  50. }
  51. return cp.exec(notifier + ' ' + options.join(' '), function (
  52. error,
  53. stdout,
  54. stderr
  55. ) {
  56. if (error) return cb(error);
  57. cb(stderr, stdout);
  58. });
  59. };
  60. module.exports.fileCommand = function (notifier, options, cb) {
  61. if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) {
  62. console.info('node-notifier debug info (fileCommand):');
  63. console.info('[notifier path]', notifier);
  64. console.info('[notifier options]', options.join(' '));
  65. }
  66. return cp.execFile(notifier, options, function (error, stdout, stderr) {
  67. if (error) return cb(error, stdout);
  68. cb(stderr, stdout);
  69. });
  70. };
  71. module.exports.fileCommandJson = function (notifier, options, cb) {
  72. if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) {
  73. console.info('node-notifier debug info (fileCommandJson):');
  74. console.info('[notifier path]', notifier);
  75. console.info('[notifier options]', options.join(' '));
  76. }
  77. return cp.execFile(notifier, options, function (error, stdout, stderr) {
  78. if (error) return cb(error, stdout);
  79. if (!stdout) return cb(error, {});
  80. try {
  81. var data = JSON.parse(stdout);
  82. cb(!stderr ? null : stderr, data);
  83. } catch (e) {
  84. cb(e, stdout);
  85. }
  86. });
  87. };
  88. module.exports.immediateFileCommand = function (notifier, options, cb) {
  89. if (process.env.DEBUG && process.env.DEBUG.indexOf('notifier') !== -1) {
  90. console.info('node-notifier debug info (notifier):');
  91. console.info('[notifier path]', notifier);
  92. }
  93. notifierExists(notifier, function (_, exists) {
  94. if (!exists) {
  95. return cb(new Error('Notifier (' + notifier + ') not found on system.'));
  96. }
  97. cp.execFile(notifier, options);
  98. cb();
  99. });
  100. };
  101. function notifierExists(notifier, cb) {
  102. return fs.stat(notifier, function (err, stat) {
  103. if (!err) return cb(err, stat.isFile());
  104. // Check if Windows alias
  105. if (path.extname(notifier)) {
  106. // Has extentioon, no need to check more
  107. return cb(err, false);
  108. }
  109. // Check if there is an exe file in the directory
  110. return fs.stat(notifier + '.exe', function (err, stat) {
  111. if (err) return cb(err, false);
  112. cb(err, stat.isFile());
  113. });
  114. });
  115. }
  116. var mapAppIcon = function (options) {
  117. if (options.appIcon) {
  118. options.icon = options.appIcon;
  119. delete options.appIcon;
  120. }
  121. return options;
  122. };
  123. var mapText = function (options) {
  124. if (options.text) {
  125. options.message = options.text;
  126. delete options.text;
  127. }
  128. return options;
  129. };
  130. var mapIconShorthand = function (options) {
  131. if (options.i) {
  132. options.icon = options.i;
  133. delete options.i;
  134. }
  135. return options;
  136. };
  137. module.exports.mapToNotifySend = function (options) {
  138. options = mapAppIcon(options);
  139. options = mapText(options);
  140. if (options.timeout === false) {
  141. delete options.timeout;
  142. }
  143. if (options.wait === true) {
  144. options['expire-time'] = 5; // 5 seconds default time (multipled below)
  145. }
  146. for (var key in options) {
  147. if (key === 'message' || key === 'title') continue;
  148. if (options.hasOwnProperty(key) && notifySendFlags[key] !== key) {
  149. options[notifySendFlags[key]] = options[key];
  150. delete options[key];
  151. }
  152. }
  153. if (typeof options['expire-time'] === 'undefined') {
  154. options['expire-time'] = 10 * 1000; // 10 sec timeout by default
  155. } else if (typeof options['expire-time'] === 'number') {
  156. options['expire-time'] = options['expire-time'] * 1000; // notify send uses milliseconds
  157. }
  158. return options;
  159. };
  160. module.exports.mapToGrowl = function (options) {
  161. options = mapAppIcon(options);
  162. options = mapIconShorthand(options);
  163. options = mapText(options);
  164. if (options.icon && !Buffer.isBuffer(options.icon)) {
  165. try {
  166. options.icon = fs.readFileSync(options.icon);
  167. } catch (ex) {}
  168. }
  169. return options;
  170. };
  171. module.exports.mapToMac = function (options) {
  172. options = mapIconShorthand(options);
  173. options = mapText(options);
  174. if (options.icon) {
  175. options.appIcon = options.icon;
  176. delete options.icon;
  177. }
  178. if (options.sound === true) {
  179. options.sound = 'Bottle';
  180. }
  181. if (options.sound === false) {
  182. delete options.sound;
  183. }
  184. if (options.sound && options.sound.indexOf('Notification.') === 0) {
  185. options.sound = 'Bottle';
  186. }
  187. if (options.wait === true) {
  188. if (!options.timeout) {
  189. options.timeout = 5;
  190. }
  191. delete options.wait;
  192. }
  193. if (!options.wait && !options.timeout) {
  194. if (options.timeout === false) {
  195. delete options.timeout;
  196. } else {
  197. options.timeout = 10;
  198. }
  199. }
  200. options.json = true;
  201. return options;
  202. };
  203. function isArray(arr) {
  204. return Object.prototype.toString.call(arr) === '[object Array]';
  205. }
  206. module.exports.isArray = isArray;
  207. function noop() {}
  208. module.exports.actionJackerDecorator = function (emitter, options, fn, mapper) {
  209. options = clone(options);
  210. fn = fn || noop;
  211. if (typeof fn !== 'function') {
  212. throw new TypeError(
  213. 'The second argument must be a function callback. You have passed ' +
  214. typeof fn
  215. );
  216. }
  217. return function (err, data) {
  218. var resultantData = data;
  219. var metadata = {};
  220. // Allow for extra data if resultantData is an object
  221. if (resultantData && typeof resultantData === 'object') {
  222. metadata = resultantData;
  223. resultantData = resultantData.activationType;
  224. }
  225. // Sanitize the data
  226. if (resultantData) {
  227. resultantData = resultantData.toLowerCase().trim();
  228. if (resultantData.match(/^activate|clicked$/)) {
  229. resultantData = 'activate';
  230. }
  231. if (resultantData.match(/^timedout$/)) {
  232. resultantData = 'timeout';
  233. }
  234. }
  235. fn.apply(emitter, [err, resultantData, metadata]);
  236. if (!mapper || !resultantData) return;
  237. var key = mapper(resultantData);
  238. if (!key) return;
  239. emitter.emit(key, emitter, options, metadata);
  240. };
  241. };
  242. module.exports.constructArgumentList = function (options, extra) {
  243. var args = [];
  244. extra = extra || {};
  245. // Massive ugly setup. Default args
  246. var initial = extra.initial || [];
  247. var keyExtra = extra.keyExtra || '';
  248. var allowedArguments = extra.allowedArguments || [];
  249. var noEscape = extra.noEscape !== undefined;
  250. var checkForAllowed = extra.allowedArguments !== undefined;
  251. var explicitTrue = !!extra.explicitTrue;
  252. var keepNewlines = !!extra.keepNewlines;
  253. var wrapper = extra.wrapper === undefined ? '"' : extra.wrapper;
  254. var escapeFn = function escapeFn(arg) {
  255. if (isArray(arg)) {
  256. return removeNewLines(arg.map(escapeFn).join(','));
  257. }
  258. if (!noEscape) {
  259. arg = escapeQuotes(arg);
  260. }
  261. if (typeof arg === 'string' && !keepNewlines) {
  262. arg = removeNewLines(arg);
  263. }
  264. return wrapper + arg + wrapper;
  265. };
  266. initial.forEach(function (val) {
  267. args.push(escapeFn(val));
  268. });
  269. for (var key in options) {
  270. if (
  271. options.hasOwnProperty(key) &&
  272. (!checkForAllowed || inArray(allowedArguments, key))
  273. ) {
  274. if (explicitTrue && options[key] === true) {
  275. args.push('-' + keyExtra + key);
  276. } else if (explicitTrue && options[key] === false) continue;
  277. else args.push('-' + keyExtra + key, escapeFn(options[key]));
  278. }
  279. }
  280. return args;
  281. };
  282. function removeNewLines(str) {
  283. var excapedNewline = process.platform === 'win32' ? '\\r\\n' : '\\n';
  284. return str.replace(/\r?\n/g, excapedNewline);
  285. }
  286. /*
  287. ---- Options ----
  288. [-t] <title string> | Displayed on the first line of the toast.
  289. [-m] <message string> | Displayed on the remaining lines, wrapped.
  290. [-b] <button1;button2 string>| Displayed on the bottom line, can list multiple buttons separated by ";"
  291. [-tb] | Displayed a textbox on the bottom line, only if buttons are not presented.
  292. [-p] <image URI> | Display toast with an image, local files only.
  293. [-id] <id> | sets the id for a notification to be able to close it later.
  294. [-s] <sound URI> | Sets the sound of the notifications, for possible values see http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx.
  295. [-silent] | Don't play a sound file when showing the notifications.
  296. [-appID] <App.ID> | Don't create a shortcut but use the provided app id.
  297. [-pid] <pid> | Query the appid for the process <pid>, use -appID as fallback. (Only relevant for applications that might be packaged for the store)
  298. [-pipeName] <\.\pipe\pipeName\> | Provide a name pipe which is used for callbacks.
  299. [-application] <C:\foo.exe> | Provide a application that might be started if the pipe does not exist.
  300. -close <id> | Closes a currently displayed notification.
  301. */
  302. var allowedToasterFlags = [
  303. 't',
  304. 'm',
  305. 'b',
  306. 'tb',
  307. 'p',
  308. 'id',
  309. 's',
  310. 'silent',
  311. 'appID',
  312. 'pid',
  313. 'pipeName',
  314. 'close',
  315. 'install'
  316. ];
  317. var toasterSoundPrefix = 'Notification.';
  318. var toasterDefaultSound = 'Notification.Default';
  319. module.exports.mapToWin8 = function (options) {
  320. options = mapAppIcon(options);
  321. options = mapText(options);
  322. if (options.icon) {
  323. if (/^file:\/+/.test(options.icon)) {
  324. // should parse file protocol URL to path
  325. options.p = new url.URL(options.icon).pathname
  326. .replace(/^\/(\w:\/)/, '$1')
  327. .replace(/\//g, '\\');
  328. } else {
  329. options.p = options.icon;
  330. }
  331. delete options.icon;
  332. }
  333. if (options.message) {
  334. // Remove escape char to debug "HRESULT : 0xC00CE508" exception
  335. options.m = options.message.replace(/\x1b/g, '');
  336. delete options.message;
  337. }
  338. if (options.title) {
  339. options.t = options.title;
  340. delete options.title;
  341. }
  342. if (options.appName) {
  343. options.appID = options.appName;
  344. delete options.appName;
  345. }
  346. if (typeof options.remove !== 'undefined') {
  347. options.close = options.remove;
  348. delete options.remove;
  349. }
  350. if (options.quiet || options.silent) {
  351. options.silent = options.quiet || options.silent;
  352. delete options.quiet;
  353. }
  354. if (typeof options.sound !== 'undefined') {
  355. options.s = options.sound;
  356. delete options.sound;
  357. }
  358. if (options.s === false) {
  359. options.silent = true;
  360. delete options.s;
  361. }
  362. // Silent takes precedence. Remove sound.
  363. if (options.s && options.silent) {
  364. delete options.s;
  365. }
  366. if (options.s === true) {
  367. options.s = toasterDefaultSound;
  368. }
  369. if (options.s && options.s.indexOf(toasterSoundPrefix) !== 0) {
  370. options.s = toasterDefaultSound;
  371. }
  372. if (options.actions && isArray(options.actions)) {
  373. options.b = options.actions.join(';');
  374. delete options.actions;
  375. }
  376. for (var key in options) {
  377. // Check if is allowed. If not, delete!
  378. if (
  379. options.hasOwnProperty(key) &&
  380. allowedToasterFlags.indexOf(key) === -1
  381. ) {
  382. delete options[key];
  383. }
  384. }
  385. return options;
  386. };
  387. module.exports.mapToNotifu = function (options) {
  388. options = mapAppIcon(options);
  389. options = mapText(options);
  390. if (options.icon) {
  391. options.i = options.icon;
  392. delete options.icon;
  393. }
  394. if (options.message) {
  395. options.m = options.message;
  396. delete options.message;
  397. }
  398. if (options.title) {
  399. options.p = options.title;
  400. delete options.title;
  401. }
  402. if (options.time) {
  403. options.d = options.time;
  404. delete options.time;
  405. }
  406. if (options.q !== false) {
  407. options.q = true;
  408. } else {
  409. delete options.q;
  410. }
  411. if (options.quiet === false) {
  412. delete options.q;
  413. delete options.quiet;
  414. }
  415. if (options.sound) {
  416. delete options.q;
  417. delete options.sound;
  418. }
  419. if (options.t) {
  420. options.d = options.t;
  421. delete options.t;
  422. }
  423. if (options.type) {
  424. options.t = sanitizeNotifuTypeArgument(options.type);
  425. delete options.type;
  426. }
  427. return options;
  428. };
  429. module.exports.isMac = function () {
  430. return os.type() === 'Darwin';
  431. };
  432. module.exports.isMountainLion = function () {
  433. return (
  434. os.type() === 'Darwin' &&
  435. semver.satisfies(garanteeSemverFormat(os.release()), '>=12.0.0')
  436. );
  437. };
  438. module.exports.isWin8 = function () {
  439. return (
  440. os.type() === 'Windows_NT' &&
  441. semver.satisfies(garanteeSemverFormat(os.release()), '>=6.2.9200')
  442. );
  443. };
  444. module.exports.isWSL = function () {
  445. return isWSL;
  446. };
  447. module.exports.isLessThanWin8 = function () {
  448. return (
  449. os.type() === 'Windows_NT' &&
  450. semver.satisfies(garanteeSemverFormat(os.release()), '<6.2.9200')
  451. );
  452. };
  453. function garanteeSemverFormat(version) {
  454. if (version.split('.').length === 2) {
  455. version += '.0';
  456. }
  457. return version;
  458. }
  459. function sanitizeNotifuTypeArgument(type) {
  460. if (typeof type === 'string' || type instanceof String) {
  461. if (type.toLowerCase() === 'info') return 'info';
  462. if (type.toLowerCase() === 'warn') return 'warn';
  463. if (type.toLowerCase() === 'error') return 'error';
  464. }
  465. return 'info';
  466. }
  467. module.exports.createNamedPipe = (server) => {
  468. const buf = Buffer.alloc(BUFFER_SIZE);
  469. return new Promise((resolve) => {
  470. server.instance = net.createServer((stream) => {
  471. stream.on('data', (c) => {
  472. buf.write(c.toString());
  473. });
  474. stream.on('end', () => {
  475. server.instance.close();
  476. });
  477. });
  478. server.instance.listen(server.namedPipe, () => {
  479. resolve(buf);
  480. });
  481. });
  482. };