notificationcenter.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * A Node.js wrapper for terminal-notify (with fallback).
  3. */
  4. var path = require('path'),
  5. notifier = path.join(__dirname, '../vendor/terminal-notifier.app/Contents/MacOS/terminal-notifier'),
  6. utils = require('../lib/utils'),
  7. Growl = require('./growl'),
  8. cloneDeep = require('lodash.clonedeep');
  9. var EventEmitter = require('events').EventEmitter;
  10. var util = require('util');
  11. var errorMessageOsX = 'You need Mac OS X 10.8 or above to use NotificationCenter,' +
  12. ' or use Growl fallback with constructor option {withFallback: true}.';
  13. module.exports = NotificationCenter;
  14. function NotificationCenter (options) {
  15. options = cloneDeep(options || {});
  16. if (!(this instanceof NotificationCenter)) {
  17. return new NotificationCenter(options);
  18. }
  19. this.options = options;
  20. EventEmitter.call(this);
  21. }
  22. util.inherits(NotificationCenter, EventEmitter);
  23. var activeId = null;
  24. NotificationCenter.prototype.notify = function (options, callback) {
  25. var fallbackNotifier = null, id = identificator();
  26. options = cloneDeep(options || {});
  27. activeId = id;
  28. if (typeof options === 'string') options = {
  29. title: 'node-notifier',
  30. message: options
  31. };
  32. callback = callback || function () {};
  33. var actionJackedCallback = utils.actionJackerDecorator(this, options, callback, function (data) {
  34. if (activeId !== id) return false;
  35. if (data === 'activate') {
  36. return 'click';
  37. }
  38. if (data === 'timeout') {
  39. return 'timeout';
  40. }
  41. return false;
  42. });
  43. options = utils.mapToMac(options);
  44. if (!!options.wait) {
  45. options.wait = 'YES';
  46. }
  47. if (!options.message && !options.group && !options.list && !options.remove) {
  48. callback(new Error('Message, group, remove or list property is required.'));
  49. return this;
  50. }
  51. var argsList = utils.constructArgumentList(options);
  52. if(utils.isMountainLion()) {
  53. utils.fileCommand(this.options.customPath || notifier, argsList, actionJackedCallback);
  54. return this;
  55. }
  56. if (fallbackNotifier || !!this.options.withFallback) {
  57. fallbackNotifier = fallbackNotifier || new Growl(this.options);
  58. return fallbackNotifier.notify(options, callback);
  59. }
  60. callback(new Error(errorMessageOsX));
  61. return this;
  62. };
  63. function identificator () {
  64. return { _ref: 'val' };
  65. }