1 |
|
2 |
|
3 |
|
4 | var path = require('path');
|
5 | var notifier = path.resolve(__dirname, '../vendor/snoreToast/SnoreToast.exe');
|
6 | var utils = require('../lib/utils');
|
7 | var Balloon = require('./balloon');
|
8 |
|
9 | var EventEmitter = require('events').EventEmitter;
|
10 | var util = require('util');
|
11 |
|
12 | var fallback = void 0;
|
13 |
|
14 | module.exports = WindowsToaster;
|
15 |
|
16 | function WindowsToaster(options) {
|
17 | options = utils.clone(options || {});
|
18 | if (!(this instanceof WindowsToaster)) {
|
19 | return new WindowsToaster(options);
|
20 | }
|
21 |
|
22 | this.options = options;
|
23 |
|
24 | EventEmitter.call(this);
|
25 | }
|
26 | util.inherits(WindowsToaster, EventEmitter);
|
27 |
|
28 | function noop() {}
|
29 |
|
30 | var timeoutMessage = 'the toast has timed out';
|
31 | var successMessage = 'user clicked on the toast';
|
32 |
|
33 | function hasText(str, txt) {
|
34 | return str && str.indexOf(txt) !== -1;
|
35 | }
|
36 |
|
37 | WindowsToaster.prototype.notify = function(options, callback) {
|
38 | options = utils.clone(options || {});
|
39 | callback = callback || noop;
|
40 |
|
41 | if (typeof options === 'string') {
|
42 | options = { title: 'node-notifier', message: options };
|
43 | }
|
44 |
|
45 | if (typeof callback !== 'function') {
|
46 | throw new TypeError(
|
47 | 'The second argument must be a function callback. You have passed ' +
|
48 | typeof fn
|
49 | );
|
50 | }
|
51 |
|
52 | var actionJackedCallback = utils.actionJackerDecorator(
|
53 | this,
|
54 | options,
|
55 | function cb(err, data) {
|
56 |
|
57 | if (err && hasText(data, timeoutMessage)) {
|
58 | return callback(null, data);
|
59 | }
|
60 | callback(err, data);
|
61 | },
|
62 | function mapper(data) {
|
63 | if (hasText(data, successMessage)) {
|
64 | return 'click';
|
65 | }
|
66 | if (hasText(data, timeoutMessage)) {
|
67 | return 'timeout';
|
68 | }
|
69 | return false;
|
70 | }
|
71 | );
|
72 |
|
73 | options.title = options.title || 'Node Notification:';
|
74 | if (
|
75 | typeof options.message === 'undefined' &&
|
76 | typeof options.close === 'undefined'
|
77 | ) {
|
78 | callback(new Error('Message or ID to close is required.'));
|
79 | return this;
|
80 | }
|
81 |
|
82 | if (!utils.isWin8() && !utils.isWSL() && !!this.options.withFallback) {
|
83 | fallback = fallback || new Balloon(this.options);
|
84 | return fallback.notify(options, callback);
|
85 | }
|
86 |
|
87 | options = utils.mapToWin8(options);
|
88 | var argsList = utils.constructArgumentList(options, {
|
89 | explicitTrue: true,
|
90 | wrapper: '',
|
91 | keepNewlines: true,
|
92 | noEscape: true
|
93 | });
|
94 | utils.fileCommand(
|
95 | this.options.customPath || notifier,
|
96 | argsList,
|
97 | actionJackedCallback
|
98 | );
|
99 | return this;
|
100 | };
|