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
|
define.class(function(require){
var FileWatcher = require('./filewatcher')
var ExternalApps = require('./externalapps')
var child_process = require('child_process')
this.atConstructor = function(args){
this.args = args
this.restart_count = 0
this.watcher = new FileWatcher()
this.only_when_down = {}
this.watcher.atChange = function(file){
if(this.child){
if(this.only_when_down[file]) return
console.color('~g~Got filechange: ~y~'+file+'~~ killing and restarting server\n')
this.child.kill('SIGHUP')
setTimeout(function(){
if(this.child) this.child.kill('SIGTERM')
}.bind(this), 50)
}
else{
console.color('~g~Got filechange: ~y~'+file+'~~ starting server\n')
this.start()
}
}.bind(this)
this.start()
}
this.restart_delay = 1000
this.start = function(){
var subarg = process.argv.slice(1)
subarg.push('-nomoni')
subarg.push('-count')
subarg.push(this.restart_count++)
subarg.unshift('--harmony')
var stdio = [process.stdin, process.stdout,'pipe']
this.was_exception = false
this.watcher.watch(subarg[1])
this.child = child_process.spawn(process.execPath, subarg, {
stdio: stdio
})
this.child.stderr.on('data', function(buf){
var data = buf.toString()
if(data.indexOf('\x0F')!= -1){
var files = data.split('\x0F')
for(var i = 0;i<files.length;i++){
var file = files[i].replace(/\n/,'')
if(file){
if(file.charAt(0) == '!'){
file = file.slice(1)
this.only_when_down[file] = 1
}
this.watcher.watch(file)
}
}
return
}
this.was_exception = data.indexOf('\0xE')!= -1? false: true
var m = data.match(/(\/[^\:]+)\:(\d+)\n/)
var ln = data.split(/\n/)
if(m){
if(this.args['-notify']) ExternalApps.notify(ln[1]+'\n'+ln[2],ln[3])
if(this.args['-edit']) ExternalApps.editor(m[1],m[2])
}
process.stdout.write(data)
}.bind(this))
this.child.on('close', function(code){
this.child = undefined
if(this.was_exception && !this.args['-restart']) return console.log("Exception detected, not restarting")
if(Date.now() - this.last >= this.restart_delay){
this.start()
}
else{
setTimeout(function(){
this.start()
}.bind(this), this.restart_delay)
}
}.bind(this))
}
})
|