forked from EdgeApp/edge-react-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execKeepAlive.js
102 lines (90 loc) · 2.27 KB
/
execKeepAlive.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
// @flow
const childProcess = require('child_process')
const argv = process.argv
main()
function pad(num, size) {
let s = num + ''
while (s.length < size) {
s = '0' + s
}
return s
}
async function main() {
let code = 0
try {
if (argv.length === 4) {
code = await callAsync(argv[2], argv[3])
} else if (argv.length === 3) {
code = await callAsync(argv[2])
}
} catch (e) {
console.log(e)
}
process.exit(code)
}
async function callAsync(cmdString, pipeCmd = '') {
return new Promise((resolve, reject) => {
const cmdArray = cmdString.split(' ')
const pipeArray = pipeCmd.split(' ')
let outCmd
let inCmd
if (pipeCmd) {
inCmd = childProcess.spawn(cmdArray[0], cmdArray.slice(1))
outCmd = childProcess.spawn(pipeArray[0], pipeArray.slice(1))
} else {
outCmd = childProcess.spawn(cmdArray[0], cmdArray.slice(1))
}
let timeoutId
const resetT = () => {
if (timeoutId) {
clearTimeout(timeoutId)
}
timeoutId = setTimeout(() => {
const date = new Date(Date.now())
const year = date.getFullYear()
const month = pad(date.getMonth() + 1, 2)
const day = pad(date.getDate(), 2)
const hour = pad(date.getHours(), 2)
const mins = pad(date.getMinutes(), 2)
console.log(`${year}-${month}-${day}-${hour}:${mins} Waiting...`)
resetT()
}, 60000)
}
if (inCmd) {
inCmd.stdout.on('data', data => {
outCmd && outCmd.stdin.write(data)
})
inCmd.stderr.on('data', data => {
console.log(data.toString())
})
inCmd.on('close', code => {
if (code === 0) {
console.log(`process exited with code ${code}`)
outCmd && outCmd.stdin.end()
} else {
resolve(code)
}
})
}
outCmd.stdout.on('data', data => {
console.log(data.toString())
resetT()
})
outCmd.stderr.on('data', data => {
console.log(data.toString())
resetT()
})
outCmd.on('close', code => {
if (code === 0) {
console.log(`process exited with code ${code}`)
if (timeoutId) {
clearTimeout(timeoutId)
}
resolve(code)
} else {
resolve(code)
}
})
resetT()
})
}