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,16 +196,22 @@
};
watch = function(source, base) {
return fs.watchFile(source, {
persistent: true,
interval: 500
}, function(curr, prev) {
if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
return;
}
return fs.readFile(source, function(err, code) {
if (err) throw err;
return compileScript(source, code.toString(), base);
return fs.stat(source, function(err, prevStats) {
if (err) throw err;
return fs.watch(source, function(event) {
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);
});
});
}
});
});
};

View File

@ -168,15 +168,22 @@ loadRequires = ->
require req for req in opts.require
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,
# such as `--lint` or `--print`.
watch = (source, base) ->
fs.watchFile source, {persistent: true, interval: 500}, (curr, prev) ->
return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
fs.readFile source, (err, code) ->
throw err if err
compileScript(source, code.toString(), base)
fs.stat source, (err, prevStats)->
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) ->
throw err if err
compileScript(source, code.toString(), base)
# Write out a JavaScript source file with the compiled code. By default, files
# are written out in `cwd` as `.js` files with the same name, but the output