Fixes #1853 -- fs.watch trouble.

This commit is contained in:
Jeremy Ashkenas
2011-12-18 09:05:42 -05:00
parent 38b6a43e26
commit 5bca978444
2 changed files with 52 additions and 41 deletions

View File

@@ -94,7 +94,10 @@
if (topLevel && !exists && source.slice(-7) !== '.coffee') {
return compile("" + source + ".coffee", sourceIndex, topLevel);
}
if (topLevel && !exists) throw new Error("File not found: " + source);
if (topLevel && !exists) {
console.error("File not found: " + source);
process.exit(1);
}
return fs.stat(source, function(err, stats) {
if (err) throw err;
if (stats.isDirectory()) {
@@ -198,30 +201,31 @@
};
watch = function(source, base) {
if (!fs.watch) return;
return fs.stat(source, function(err, prevStats) {
var callback, watcher;
if (err) throw err;
return watcher = fs.watch(source, callback = function(event) {
if (event === 'rename') {
watcher.close();
try {
return watcher = fs.watch(source, callback);
} catch (_error) {}
} else if (event === 'change') {
return fs.stat(source, function(err, stats) {
if (err) throw err;
if (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
return;
}
prevStats = stats;
return fs.readFile(source, function(err, code) {
if (err) throw err;
return compileScript(source, code.toString(), base);
});
});
var callback, compile, prevStats, watcher;
prevStats = null;
compile = function() {
return fs.stat(source, function(err, stats) {
if (err) throw err;
if (prevStats && (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime())) {
return;
}
prevStats = stats;
return fs.readFile(source, function(err, code) {
if (err) throw err;
return compileScript(source, code.toString(), base);
});
});
};
return watcher = fs.watch(source, callback = function(event) {
if (event === 'change') {
return compile();
} else if (event === 'rename') {
watcher.close();
return setTimeout(function() {
compile();
return watcher = fs.watch(source, callback);
}, 250);
}
});
};