Permalink
Browse files

permit a comma delimited list of js files or folders to be watched

  • Loading branch information...
1 parent 2f63642 commit 9ce652b38d43de402cc65a3a007883c67c84bdc8 @codelotus codelotus committed May 22, 2010
Showing with 30 additions and 23 deletions.
  1. +30 −23 lib/supervisor.js
View
@@ -6,7 +6,6 @@ var spawn = require("child_process").spawn;
exports.run = run;
function run (args) {
- sys.debug("args: "+args);
var arg, next, watch, program;
while (arg = args.shift()) {
if (arg === "--help" || arg === "-h" || arg === "-?") {
@@ -17,39 +16,47 @@ function run (args) {
watch = 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.");
}
+
startProgram(program);
if(watch) {
- if(!watch.match(/^\/.*/)) { // watch is not an absolute path
- // convert watch item to absolute path
- watch = process.cwd() + '/' + watch;
- }
- findAllJsFiles(watch, watchFile);
+ var watchItems = watch.split(',');
+ watchItems.forEach(function (watchItem) {
+ 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);
+ });
}
else {
sys.error("No watch dir or file specified.");
throw new Error("No watch dir or file specified");
}
+
};
function print (m, n) { sys.print(m+(!n?"\n":"")); return print }
function help () {
print
("Supervisor is used to restart programs when they crash.")
- ("It can also be used to restart programs when a folder or file changes.")
+ ("It can also be used to restart programs when a *.js file changes.")
("Usage:")
(" supervisor [options]")
("")
("Options:")
- (" -w|--watch <folder>")
- (" Watch a folder for changes. When a change occurs, reload the program")
+ (" -w|--watch <watchItems>")
+ (" A comma delimited list of folders or js files to watch for changes.")
+ (" When a change to a js file occurs, reload the program")
("")
(" -p|--program <program>")
(" The program to run")
@@ -89,33 +96,33 @@ function stopCrashing () {
else counter = -1;
}
-function watchFile (watch) {
+function watchGivenFile (watch) {
fs.watchFile(watch, crash);
}
var findAllJsFiles = function(path, callback) {
- fs.readdir(path, function(err, fileNames) {
+ fs.stat(path, function(err, stats){
if(err) {
- sys.error('Error reading path: ' + path);
+ sys.puts('Error retrieving stats for file: ' + path);
}
else {
- fileNames.forEach(function (fileName) {
- fs.stat(path + '/' + fileName, function(err, stats) {
+ if(stats.isDirectory()) {
+ fs.readdir(path, function(err, fileNames) {
if(err) {
- sys.error('Error retrieving stats for file: ' + path + '/' + fileName);
+ sys.puts('Error reading path: ' + path);
}
else {
- if(stats.isDirectory()) {
+ fileNames.forEach(function (fileName) {
findAllJsFiles(path + '/' + fileName, callback);
- }
- else {
- if(fileName.match(/.*\.js$/)) {
- callback(path + '/' + fileName);
- }
- }
+ });
}
});
- });
+ }
+ else {
+ if(path.match(/.*\.js$/)) {
+ callback(path);
+ }
+ }
}
});
}

0 comments on commit 9ce652b

Please sign in to comment.