1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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) { 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) {
return; if (err) throw err;
} if (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
return fs.readFile(source, function(err, code) { return;
if (err) throw err; }
return compileScript(source, code.toString(), base); 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 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.readFile source, (err, code) -> fs.watch source, (event) ->
throw err if err if event is 'change'
compileScript(source, code.toString(), base) 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 # 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 # are written out in `cwd` as `.js` files with the same name, but the output