mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Fixes #1853 -- fs.watch trouble.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user