-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_helper.js
More file actions
327 lines (290 loc) · 9.32 KB
/
node_helper.js
File metadata and controls
327 lines (290 loc) · 9.32 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* MagicMirror²
* Module: MMM-CommandToNotification
*
* By Tom Hirschberger
* MIT Licensed.
*/
const Log = require("logger")
const NodeHelper = require("node_helper")
const spawnSync = require("node:child_process").spawnSync
const spawn = require("node:child_process").spawn
const fs = require("node:fs")
const path = require("node:path")
const scriptsDir = path.join(__dirname, "/scripts")
module.exports = NodeHelper.create({
start() {
const self = this
self.started = false
self.cmdSkips = {}
self.asyncOutput = {}
},
sleep(milliseconds) {
return new Promise((resolve) => { setTimeout(resolve, milliseconds) })
},
// https://stackoverflow.com/questions/14332721/node-js-spawn-child-process-and-get-terminal-output-live
runScript(command, args, options, cmdIdx, curCmdConfig, curNotifications) {
const self = this
const child = spawn(command, args, options)
let scriptOutput = null
child.stdout.setEncoding("utf8")
child.stdout.on("data", (data) => {
if (scriptOutput === null) {
scriptOutput = ""
}
data = data.toString()
scriptOutput += data
})
child.on("close", (code) => {
if (scriptOutput !== null) {
scriptOutput = scriptOutput.trim()
}
self.postProcessCommand(cmdIdx, curCmdConfig, curNotifications, scriptOutput, code)
})
},
validateConditions(curCmdConfig, output, returnCode) {
if (typeof curCmdConfig.conditions !== "undefined") {
if (typeof curCmdConfig.conditions.returnCode !== "undefined") {
let matched = false
let returnCodeConfig = curCmdConfig.conditions.returnCode
if (!Array.isArray(returnCodeConfig)) {
returnCodeConfig = [returnCodeConfig]
}
for (let idx = 0; idx < returnCodeConfig.length; idx++) {
if (returnCodeConfig[idx] === returnCode) {
matched = true
break
}
}
if (!matched) {
return false
}
}
if (typeof curCmdConfig.conditions.outputContains !== "undefined") {
let matched = false
let outputConfig = curCmdConfig.conditions.outputContains
if (!Array.isArray(outputConfig)) {
outputConfig = [outputConfig]
}
for (let idx = 0; idx < outputConfig.length; idx++) {
if (output.includes(outputConfig[idx])) {
matched = true
break
}
}
if (!matched) {
return false
}
}
return true
} else {
return true
}
},
validateAndModifyPath(thePath) {
let newPath
let testPath
if (thePath.startsWith("/")) {
// absolute path. Nothing to do
newPath = thePath
testPath = newPath
} else if (thePath.indexOf("/") > 0) {
// path contains a "/". lets check if it is a relative one
if (thePath.startsWith("./")) {
// relative starting at the current directory
newPath = thePath
testPath = scriptsDir + thePath.substring(1)
} else if (thePath.startsWith("../")) {
// relative targeting the directory one up
newPath = thePath
testPath = `${scriptsDir}/${thePath}`
} else {
// its a relative path so we add a "./"
newPath = `./${thePath}`
testPath = `${scriptsDir}/${thePath}`
}
} else {
// as it is a path without any "/" it is either a script in the scripts directory or a command in $PATH
// lets check if the script exists in the script directory
testPath = `${scriptsDir}/${thePath}`
if (fs.existsSync(testPath)) {
// its a script in the scripts dir
newPath = testPath
} else {
// it might be a command in $PATH but we can not test it
newPath = thePath
testPath = null
}
}
if (testPath !== null) {
try {
testPath = fs.realpathSync(testPath)
} catch { /* Keep the old value */ }
}
return { thePath: newPath, testPath }
},
async callCommands() {
const self = this
Log.log(`${this.name}: Calling commands`)
for (let cmdIdx = 0; cmdIdx < self.config.commands.length; cmdIdx++) {
const curCmdConfig = self.config.commands[cmdIdx]
if (typeof curCmdConfig.script !== "undefined") {
if ((typeof curCmdConfig.skips === "undefined") || (curCmdConfig.skips < self.cmdSkips[cmdIdx])
) {
self.cmdSkips[cmdIdx] = 1
const curCommand = curCmdConfig.script
const modCommand = self.validateAndModifyPath(curCommand)
if ((modCommand.testPath === null) || fs.existsSync(modCommand.testPath)) {
let isExecutable = true
if (modCommand.testPath !== null) {
try {
fs.accessSync(modCommand.testPath, fs.constants.X_OK)
} catch {
isExecutable = false
}
}
if (isExecutable) {
let curArgs = curCmdConfig.args
if (typeof curCmdConfig.args !== "undefined") {
if (Array.isArray(curCmdConfig.args)) {
curArgs = curCmdConfig.args
} else {
curArgs = curCmdConfig.args.split(" ")
}
}
const curNotifications = curCmdConfig.notifications
let curEncoding = "utf8"
if (typeof curCmdConfig.encoding !== "undefined") {
curEncoding = curCmdConfig.encoding
}
const options = {
shell: true,
encoding: curEncoding,
cwd: scriptsDir,
}
if (typeof curCmdConfig.timeout !== "undefined") {
options.timeout = curCmdConfig.timeout
}
let output
let returnCode
let sync = self.config.sync
if (typeof curCmdConfig.sync !== "undefined") {
sync = curCmdConfig.sync
}
try {
if (sync) {
if (self.config.debug) {
Log.log(`${self.name}: Running ${modCommand.thePath} synchronous`)
}
const spawnOutput = spawnSync(modCommand.thePath, curArgs, options)
returnCode = spawnOutput.status
output = spawnOutput.stdout
if (output !== null) {
output = output.trim()
}
if (returnCode === null) {
returnCode = 1
output += "Timeout"
}
self.postProcessCommand(cmdIdx, curCmdConfig, curNotifications, output, returnCode)
} else {
if (self.config.debug) {
Log.log(`${self.name}: Running ${curCommand} asynchronous`)
}
self.runScript(modCommand.thePath, curArgs, options, cmdIdx, curCmdConfig, curNotifications)
}
} catch (error) {
Log.log(`${self.name}: Error during call of command with index ${cmdIdx}`)
Log.log(error)
}
} else {
Log.log(`${self.name}: The command with index ${cmdIdx} could not be executed. The file ${modCommand.testPath} is not executable!`)
}
} else {
Log.log(`${self.name}: The command with index ${cmdIdx} could not be executed. The file ${modCommand.testPath} does not exist!`)
}
if (curCmdConfig.delayNext || false) {
if (self.config.debug) {
Log.log(`${self.name}: Delaying next: ${curCmdConfig.delayNext}`)
}
await self.sleep(curCmdConfig.delayNext)
}
} else {
self.cmdSkips[cmdIdx] += 1
}
} else {
Log.log(`${self.name}: The command with index ${cmdIdx} is not configured properly. It is missing the script configuration option!`)
}
}
},
postProcessCommand(cmdIdx, curCmdConfig, curNotifications, output, returnCode) {
const self = this
if (self.config.debug) {
Log.log(`${self.name}: Postprocessing cmdIdx: ${cmdIdx}`)
Log.log(`${self.name}: curCmdConfig: ${JSON.stringify(curCmdConfig)}`)
Log.log(output)
Log.log(`Return code: ${returnCode}`)
}
if (output !== null) {
if (typeof curNotifications !== "undefined") {
if (self.validateConditions(curCmdConfig, output, returnCode)) {
for (let curNotiIdx = 0; curNotiIdx < curNotifications.length; curNotiIdx++) {
if (Array.isArray(curNotifications[curNotiIdx])) {
if (curNotifications[curNotiIdx].length > 1) {
self.sendSocketNotification(`RESULT_${curNotifications[curNotiIdx][0]}`, curNotifications[curNotiIdx][1])
} else {
self.sendSocketNotification(`RESULT_${curNotifications[curNotiIdx][0]}`, output)
}
} else {
self.sendSocketNotification(`RESULT_${curNotifications[curNotiIdx]}`, output)
}
}
}
} else {
Log.log(`${self.name}: The command with idx: ${cmdIdx} has no notifications configured. It had been called but no notification will be send!`)
}
}
},
setDefaultsAndStart() {
const self = this
if (self.started) {
if (self.config.debug) {
Log.log(`${self.name}: Setting the defaults`)
}
if (typeof self.config.commands !== "undefined") {
for (let cmdIdx = 0; cmdIdx < self.config.commands.length; cmdIdx++) {
const curCmdConfig = self.config.commands[cmdIdx]
if (typeof curCmdConfig.skips !== "undefined") {
self.cmdSkips[cmdIdx] = curCmdConfig.skips + 1
}
}
if (self.config.debug) {
Log.log(`${self.name}: Call the commands initially`)
}
self.callCommands()
if (self.config.debug) {
Log.log(`${self.name}: Reschedule the next calls`)
}
self.rescheduleCommandCall()
}
} else {
Log.log(`${self.name}: Defaults can not be set because module is not started at the moment`)
}
},
rescheduleCommandCall() {
const self = this
return new Promise(() => {
setTimeout(() => {
self.callCommands()
self.rescheduleCommandCall()
}, self.config.updateInterval * 1000)
})
},
socketNotificationReceived(notification, payload) {
const self = this
if (notification === "CONFIG" && self.started === false) {
self.config = payload
self.started = true
self.setDefaultsAndStart()
}
}
})