-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_dev_server.js
87 lines (64 loc) · 2.18 KB
/
run_dev_server.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
/*
* This file is part of the Spludo Framework.
* Copyright (c) 2009-2010 DracoBlue, http://dracoblue.net/
*
* Licensed under the terms of MIT License. For the full copyright and license
* information, please see the LICENSE file in the root folder.
*/
var child_process = require('child_process');
var fs = require("fs");
var sys = require("sys");
dev_server = {
process: null,
files: [],
restarting: false,
"restart": function() {
this.restarting = true;
sys.debug('DEVSERVER: Stopping server for restart');
this.process.kill();
},
"start": function() {
var self = this;
sys.debug('DEVSERVER: Starting server');
self.watchFiles();
this.process = child_process.spawn(process.ARGV[0], ['server.js']);
this.process.stdout.addListener('data', function (data) {
process.stdout.write(data);
});
this.process.stderr.addListener('data', function (data) {
sys.print(data);
});
this.process.addListener('exit', function (code) {
sys.debug('DEVSERVER: Child process exited: ' + code);
this.process = null;
if (self.restarting) {
self.restarting = true;
self.unwatchFiles();
self.start();
}
});
},
"watchFiles": function() {
var self = this;
child_process.exec('find . | grep "\.js$"', function(error, stdout,
stderr) {
var files = stdout.trim().split("\n");
files.forEach(function(file) {
self.files.push(file);
fs.watchFile(file, {interval : 500}, function(curr, prev) {
if (curr.mtime.valueOf() != prev.mtime.valueOf() || curr.ctime.valueOf() != prev.ctime.valueOf()) {
sys.debug('DEVSERVER: Restarting because of changed file at ' + file);
dev_server.restart();
}
});
});
});
},
"unwatchFiles": function() {
this.files.forEach(function(file) {
fs.unwatchFile(file);
});
this.files = [];
}
}
dev_server.start();