/
index.js
137 lines (108 loc) · 3.23 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
By Tomas Pollak <tomas@forkhq.com>.
MIT License.
*/
var join = require('path').join,
spawn = require('child_process').spawn;
var Dialog = module.exports = {
err: function(str, title, callback) {
this.show('error', str, title, callback);
},
info: function(str, title, callback) {
this.show('info', str, title, callback);
},
warn: function(str, title, callback) {
this.show('warning', str, title, callback);
},
show: function(type, str, title, callback) {
if (!str || str.trim() == '')
throw new Error('Empty or no string passed!');
if (typeof title == 'function') {
callback = title;
title = null;
}
var cmd = [],
os_name = process.platform,
title = title ? title : 'Important';
var str = (str + '').replace(/([.?*+^$[\]\\(){}<>|`-])/g, "\$1");
// return codes for zenity are 0 on "OK"
// and 1 on "No/Cancel" or close window
if (os_name == 'linux') {
str = str.replace(/[<>]/g, '');
cmd.push('zenity');
cmd.push('--' + type);
cmd.push('--text') && cmd.push(str);
cmd.push('--title') && cmd.push(title);
if (str.length > 30) cmd.push('--width') && cmd.push('300');
// return codes in macOS are exactly as in Linux
// 0 for 'OK', 1 for 'Cancel'
} else if (os_name == 'darwin') {
switch (type) { // Set dialog icon
case 'error':
type = 0;
break;
case 'info':
type = 1;
break;
case 'warning':
type = 2;
break;
default:
type = '';
}
str = str.replace(/"/g, "'"); // double quotes to single quotes
cmd.push('osascript') && cmd.push('-e');
var script = 'tell app \"System Events\" to display dialog ';
script += '\"' + str + '\" with title \"' + title + '\" buttons \"OK\"';
script += ' with icon ' + type;
cmd.push(script);
} else { // windows
// return codes for Windows (default value minus 1)
// defaults in https://www.tutorialspoint.com/vbscript/vbscript_dialog_boxes.htm
// 0 - vbOK
// 1 - vbCancel
// 2 - vbAbort
// 3 - vbRetry
// 4 - vbIgnore
// 5 - vbYes
// 6 - vbNo
switch (type) { // Set MsgBox icon
case 'error':
type = 16;
break;
case 'info':
type = 64;
break;
case 'warning':
type = 48;
break;
default:
type = 0;
}
str = str.replace(/"/g, "'"); // double quotes to single quotes
// msgbox.vbs script from http://stackoverflow.com/questions/774175
cmd.push('cscript');
cmd.push(join(__dirname, 'msgbox.vbs'));
cmd.push(str) && cmd.push(type) && cmd.push(title);
}
this.run(cmd, callback);
},
run: function(cmd, cb) {
var bin = cmd[0],
args = cmd.splice(1),
stdout = '',
stderr = '';
var child = spawn(bin, args, {
detached: true
});
child.stdout.on('data', function(data) {
stdout += data.toString();
})
child.stderr.on('data', function(data) {
stderr += data.toString();
})
child.on('exit', function(code) {
cb && cb(code, stdout, stderr);
})
}
}