Skip to content

Commit

Permalink
Update supervisor to handle CoffeeScript files, with options to allow…
Browse files Browse the repository at this point in the history
… any file extension and cmd-line runner other than node
  • Loading branch information
David Taylor committed Aug 16, 2010
1 parent a576ca0 commit 79dcf0e
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions lib/supervisor.js
Expand Up @@ -6,35 +6,52 @@ var spawn = require("child_process").spawn;
exports.run = run;

function run (args) {
var arg, next, watch, program;
var arg, next, watch, program, extensions, executor;
while (arg = args.shift()) {
if (arg === "--help" || arg === "-h" || arg === "-?") {
return help();
} else if (arg === "--program" || arg === "-p") {
program = args.shift();
} else if (arg === "--watch" || arg === "-w") {
watch = args.shift();
} else if (arg === "--extensions" || arg === "-e") {
extensions = args.shift();
} else if (arg === "--exec" || arg === "-x") {
executor = args.shift();
}
}
sys.debug("watch: " + watch);

// if we have a program, then run it, and restart when it crashes.
// if we have a watch folder, then watch the folder for changes and restart the prog
if (!program) {
sys.error("No program specified.");
throw new Error("No program specified.");
}
if (!extensions) {
// If no extensions passed try to guess from the program
var fileExtMatch = program.match(/.*\.(.*)/);
extensions = fileExtMatch && fileExtMatch[1] || "js";
}
if (!executor) {
executor = (extensions === "coffee") ? "coffee" : "node";
}

sys.debug("Running with:");
sys.debug(" program: " + program);
sys.debug(" watch: " + watch);
sys.debug(" extensions: " + extensions);
sys.debug(" executor: " + executor);

startProgram(program);
if(watch) {
startProgram(program, executor);
if (watch) {
var watchItems = watch.split(',');
watchItems.forEach(function (watchItem) {
if(!watchItem.match(/^\/.*/)) { // watch is not an absolute path
if (!watchItem.match(/^\/.*/)) { // watch is not an absolute path
// convert watch item to absolute path
watchItem = process.cwd() + '/' + watchItem;
}
sys.debug("WatchItem: " + watchItem);
findAllJsFiles(watchItem, watchGivenFile);
findAllWatchFiles(watchItem, extensions, watchGivenFile);
});
} else {
sys.error("No watch dir or file specified.");
Expand Down Expand Up @@ -64,9 +81,9 @@ function help () {
(" Help")
}

function startProgram (prog) {
function startProgram (prog, exec) {
sys.debug("Starting child: "+prog);
var child = exports.child = spawn("node", [prog]);
var child = exports.child = spawn(exec, [prog]);
child.stdout.addListener("data", function (chunk) { chunk && sys.print(chunk) });
child.stderr.addListener("data", function (chunk) { chunk && sys.debug(chunk) });
child.addListener("exit", function () { startProgram(prog) });
Expand Down Expand Up @@ -99,24 +116,25 @@ function watchGivenFile (watch) {
fs.watchFile(watch, crash);
}

var findAllJsFiles = function(path, callback) {
var findAllWatchFiles = function(path, exts, callback) {
fs.stat(path, function(err, stats){
if(err) {
if (err) {
sys.puts('Error retrieving stats for file: ' + path);
} else {
if(stats.isDirectory()) {
if (stats.isDirectory()) {
fs.readdir(path, function(err, fileNames) {
if(err) {
sys.puts('Error reading path: ' + path);
}
else {
fileNames.forEach(function (fileName) {
findAllJsFiles(path + '/' + fileName, callback);
findAllWatchFiles(path + '/' + fileName, exts, callback);
});
}
});
} else {
if(path.match(/.*\.(js|node)/)) {
var regex = new RegExp(".*\.(node|" + exts + ")");
if(path.match(regex)) {
callback(path);
}
}
Expand Down

0 comments on commit 79dcf0e

Please sign in to comment.