diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index 3a2355b6..7615a42b 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -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); + }); + }); + } }); }); }; diff --git a/src/command.coffee b/src/command.coffee index c89e7d46..8fd72b66 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -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