UNPKG

2.5 kBJavaScriptView Raw
1/**
2 * Wrapper for the toaster (https://github.com/nels-o/toaster)
3 */
4var path = require('path');
5var notifier = path.resolve(__dirname, '../vendor/snoreToast/SnoreToast.exe');
6var utils = require('../lib/utils');
7var Balloon = require('./balloon');
8
9var EventEmitter = require('events').EventEmitter;
10var util = require('util');
11
12var fallback = void 0;
13
14module.exports = WindowsToaster;
15
16function 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}
26util.inherits(WindowsToaster, EventEmitter);
27
28function noop() {}
29
30var timeoutMessage = 'the toast has timed out';
31var successMessage = 'user clicked on the toast';
32
33function hasText(str, txt) {
34 return str && str.indexOf(txt) !== -1;
35}
36
37WindowsToaster.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 // Needs to filter out timeout. Not an actual error.
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};