Permalink
Browse files

Merge branch 'owiber-master'

Conflicts:
	package.json
  • Loading branch information...
2 parents 8de66d4 + ce94b67 commit 41673bb0ede8a019d321add5a151be72c18afdc9 @iangreenleaf iangreenleaf committed Jun 26, 2012
Showing with 54 additions and 17 deletions.
  1. +54 −17 lib/supervisor.js
View
@@ -1,24 +1,31 @@
var util = require("util");
var fs = require("fs");
var spawn = require("child_process").spawn;
+var path = require("path");
var fileExtensionPattern;
var startChildProcess;
var noRestartOn = null;
var debug = true;
+var verbose = false;
+var ignoredPaths = {};
exports.run = run;
function run (args) {
- var arg, next, watch, program, programArgs, extensions, executor, poll_interval;
+ var arg, next, watch, ignore, program, programArgs, extensions, executor, poll_interval;
while (arg = args.shift()) {
if (arg === "--help" || arg === "-h" || arg === "-?") {
return help();
} else if (arg === "--quiet" || arg === "-q") {
debug = false;
util.debug = function(){};
util.puts = function(){};
+ } else if (arg === "--verbose" || arg === "-V") {
+ verbose = true;
} else if (arg === "--watch" || arg === "-w") {
watch = args.shift();
+ } else if (arg === "--ignore" || arg === "-i") {
+ ignore = args.shift();
} else if (arg === "--poll-interval" || arg === "-p") {
poll_interval = parseInt(args.shift());
} else if (arg === "--extensions" || arg === "-e") {
@@ -82,6 +89,7 @@ function run (args) {
util.debug("Running node-supervisor with");
util.debug(" program '" + program.join(" ") + "'");
util.debug(" --watch '" + watch + "'");
+ util.debug(" --ignore '" + ignore + "'");
util.debug(" --extensions '" + extensions + "'");
util.debug(" --exec '" + executor + "'");
util.puts("");
@@ -94,12 +102,18 @@ function run (args) {
// if we have a watch folder, then watch the folder for changes and restart the prog
startChildProcess();
+ if (ignore) {
+ var ignoreItems = ignore.split(',');
+ ignoreItems.forEach(function (ignoreItem) {
+ ignoreItem = path.resolve(ignoreItem);
+ ignoredPaths[ignoreItem] = true;
+ util.debug("Ignoring directory '" + ignoreItem + "'.");
+ });
+ }
+
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;
- }
+ watchItem = path.resolve(watchItem);
util.debug("Watching directory '" + watchItem + "' for changes.");
findAllWatchFiles(watchItem, function(f) {
watchGivenFile( f, poll_interval );
@@ -129,6 +143,10 @@ function help () {
(" When a change to a js file occurs, reload the program")
(" Default is '.'")
("")
+ (" -i|--ignore <ignoreItems>")
+ (" A comma-delimited list of folders to ignore for changes.")
+ (" No default")
+ ("")
(" -p|--poll-interval <milliseconds>")
(" How often to poll watched files for changes.")
(" Defaults to Node default.")
@@ -154,6 +172,9 @@ function help () {
(" -q|--quiet")
(" Suppress DEBUG messages")
("")
+ (" -V|--verbose")
+ (" Show extra DEBUG messages")
+ ("")
("Examples:")
(" supervisor myapp.js")
(" supervisor myapp.coffee")
@@ -197,9 +218,20 @@ function crash () {
}, 50);
}
-function crashWin (event) {
- if( event === 'change' )
- crash();
+function crashWin (event, filename) {
+ var shouldCrash = true;
+ if( event === 'change' ) {
+ if (filename) {
+ filename = path.resolve(filename);
+ Object.keys(ignoredPaths).forEach(function (ignorePath) {
+ if ( filename.indexOf(ignorePath + '\\') === 0 ) {
+ shouldCrash = false;
+ }
+ });
+ }
+ if (shouldCrash)
+ crash();
+ }
}
function crashOther (oldStat, newStat) {
@@ -214,29 +246,34 @@ function watchGivenFile (watch, poll_interval) {
fs.watch(watch, { persistent: true, interval: poll_interval }, crashWin);
else
fs.watchFile(watch, { persistent: true, interval: poll_interval }, crashOther);
+ if (verbose)
+ util.debug("watching file '" + watch + "'");
}
-var findAllWatchFiles = function(path, callback) {
- fs.stat(path, function(err, stats){
+var findAllWatchFiles = function(dir, callback) {
+ dir = path.resolve(dir);
+ if (ignoredPaths[dir])
+ return;
+ fs.stat(dir, function(err, stats){
if (err) {
- util.error('Error retrieving stats for file: ' + path);
+ util.error('Error retrieving stats for file: ' + dir);
} else if (isWindows) {
- callback(path);
+ callback(dir);
} else {
if (stats.isDirectory()) {
- fs.readdir(path, function(err, fileNames) {
+ fs.readdir(dir, function(err, fileNames) {
if(err) {
- util.error('Error reading path: ' + path);
+ util.error('Error reading path: ' + dir);
}
else {
fileNames.forEach(function (fileName) {
- findAllWatchFiles(path + '/' + fileName, callback);
+ findAllWatchFiles(dir + '/' + fileName, callback);
});
}
});
} else {
- if (path.match(fileExtensionPattern)) {
- callback(path);
+ if (dir.match(fileExtensionPattern)) {
+ callback(dir);
}
}
}

0 comments on commit 41673bb

Please sign in to comment.