Fixes #1803 -- use fs.watch instead of fs.watchFile ... more of a PITA, but hey.

This commit is contained in:
Jeremy Ashkenas 2011-11-08 16:13:45 -05:00
parent 78491b3a58
commit d592d2c9cb
2 changed files with 29 additions and 16 deletions

View File

@ -196,18 +196,24 @@
}; };
watch = function(source, base) { watch = function(source, base) {
return fs.watchFile(source, { return fs.stat(source, function(err, prevStats) {
persistent: true, if (err) throw err;
interval: 500 return fs.watch(source, function(event) {
}, function(curr, prev) { if (event === 'change') {
if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) { return fs.stat(source, function(err, stats) {
if (err) throw err;
if (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
return; return;
} }
prevStats = stats;
return fs.readFile(source, function(err, code) { return fs.readFile(source, function(err, code) {
if (err) throw err; if (err) throw err;
return compileScript(source, code.toString(), base); return compileScript(source, code.toString(), base);
}); });
}); });
}
});
});
}; };
writeJs = function(source, js, base) { writeJs = function(source, js, base) {

View File

@ -168,12 +168,19 @@ loadRequires = ->
require req for req in opts.require require req for req in opts.require
module.filename = realFilename module.filename = realFilename
# Watch a source CoffeeScript file using `fs.watchFile`, recompiling it every # Watch a source CoffeeScript file using `fs.watch`, recompiling it every
# time the file is updated. May be used in combination with other options, # time the file is updated. May be used in combination with other options,
# such as `--lint` or `--print`. # such as `--lint` or `--print`.
watch = (source, base) -> watch = (source, base) ->
fs.watchFile source, {persistent: true, interval: 500}, (curr, prev) -> fs.stat source, (err, prevStats)->
return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime() throw err if err
fs.watch source, (event) ->
if event is 'change'
fs.stat source, (err, stats) ->
throw err if err
return if stats.size is prevStats.size and
stats.mtime.getTime() is prevStats.mtime.getTime()
prevStats = stats
fs.readFile source, (err, code) -> fs.readFile source, (err, code) ->
throw err if err throw err if err
compileScript(source, code.toString(), base) compileScript(source, code.toString(), base)