Created
October 3, 2010 20:17
-
-
Save felipeelias/608888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment