File size: 1,323 Bytes
7e79d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * A Node.js wrapper for terminal-notify.
 *
 * Requirements:
 *  - Mac OS X 10.8
 */

var path = require('path'),
    notifier = path.resolve(__dirname, '../../vendor/terminal-notifier.app/Contents/MacOS/terminal-notifier'),
    utils = require('../utils'),
    Growl = require('./growl');


var fallbackNotifier = null;
var Notifier = function (options) {
  if (!(this instanceof Notifier)) {
    return new Notifier();
  }
  this.options = options;
  this.isOSX = false;
  fallbackNotifier = null;
};

Notifier.prototype.notify = function (options, callback) {
  options = utils.mapToMac(options);

  var argsList = utils.constructArgumentList(options);
  callback = callback || function (err, data) {};

  if(this.isOSX) {
    utils.command(notifier, argsList, callback);
    return this;
  }

  if(fallbackNotifier) {
    fallbackNotifier.notify(options, callback);
    return this;
  }

  var self = this;
  utils.isMacOSX(function (error, errorMsg) {
    if (error && error !== 'old') {
      return callback(new Error(errorMsg));
    }

    if (error && error === 'old') {
      fallbackNotifier = new Growl(self.options);
      return fallbackNotifier.notify(options, callback);
    }

    utils.command(notifier, argsList, callback);
    self.isOSX = true;
  });
  return this;
};

module.exports = Notifier;