This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.js
91 lines (80 loc) · 2.11 KB
/
node.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
const { execSync, spawn } = require('child_process')
const { platform } = process
const { join } = require('path')
const isProgramInstalled = require('is-program-installed')
const windowsScript = join(__dirname, 'msgbox.vbs')
const execCmd = (cmds) => spawn(cmds[0], cmds.splice(1))
const unixPrograms = [
'kdialog',
'zenity',
'yad',
'notify-send',
'xmessage',
'dialog',
'whiptail'
]
const bestUnixProgram = unixPrograms.filter(isProgramInstalled)[0] || 'console'
const cscript = (s) => ['cscript', windowsScript, s]
const msg = (str) => ['msg', '"%username%"', str]
const zenity = (s) => ['zenity', '--info', '--text', s]
const yad = (s) => ['yad', '--text', s, '--button', 'OK']
const notifySend = (s) => ['notify-send', s]
const xmessage = (s) => ['xmessage', s]
const dialog = (s) => ['dialog', '--msgbox', s, '10', '30']
const whiptail = (s) => ['whiptail', '--msbox', s, '10', '30']
const kdialog = (s) => ['kdialog', '--msgbox', s]
const osascript = (s) => [
'osascript',
'-e',
`tell app "System Events" to display dialog "${s}" buttons "OK"`
]
const hasCscript =
platform.startsWith('win') &&
(() => {
try {
execSync('cscript')
return true
} catch {
return false
}
})()
const nameMap = {
console: console.log,
cscript,
dialog,
kdialog,
msg,
'notify-send': notifySend,
osascript,
whiptail,
xmessage,
yad,
zenity
}
const getAlert = (input = '', thingToUse = '') => {
const execInput = (cmd) => execCmd(cmd(input))
const pickFromNameMap = (option = bestUnixProgram) => {
if (option !== 'console') {
if (nameMap[option]) {
return execInput(nameMap[option])
}
}
return console.log(input)
}
if (thingToUse) {
return pickFromNameMap(thingToUse)
}
switch (platform) {
case 'linux':
case 'freebsd':
case 'sunos':
return pickFromNameMap(bestUnixProgram)
case 'darwin':
return execInput(osascript)
case 'win32':
return hasCscript ? execInput(cscript) : execInput(msg)
default:
return console.log(input)
}
}
module.exports = process.env.DISABLE_ALERT !== '1' ? getAlert : () => {}