-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitmon.js
executable file
·48 lines (46 loc) · 998 Bytes
/
gitmon.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
#!/usr/bin/env node
function delay(ms){return new Promise(resolve=>setTimeout(resolve,ms))}
var cp=require('child_process')
async function init() {
var [cmd,...args]=process.argv.slice(2)
console.log({cmd,args})
var p=exec(cmd,args)
while(true) {
var str=await execPromise('git pull')
if(!str.match(/Already up to/)) {
console.log({str})
console.log('Need to update, killing...')
p.kill()
console.log('Retarting...')
p=exec(cmd,args)
}
try {
}
catch(err){
console.log(err)
await delay(5000)
}
await delay(5000)
}
}
function execPromise(cmd) {
return new Promise(resolve=>{
var x=cp.exec(cmd)
var str=''
//@ts-ignore
x.stdout.on("data",data=>{
str+=data.toString()
})
//@ts-ignore
x.stderr.on("data", data=>{
str+=data.toString()
})
x.on('exit',()=>{
resolve(str)
})
})
}
function exec(cmd,args){
return cp.spawn(cmd,args,{stdio:'inherit'})
}
init()