diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index dd235442..a42a00a2 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -246,24 +246,23 @@ }; watch = function(source, base) { - var compile, compileTimeout, e, prevStats, rewatch, watchErr, watcher; + var compile, compileTimeout, err, prevStats, rewatch, watchErr, watcher; + watcher = null; prevStats = null; compileTimeout = null; - watchErr = function(e) { - if (e.code === 'ENOENT') { - if (sources.indexOf(source) === -1) { - return; - } - try { - rewatch(); - return compile(); - } catch (_error) { - e = _error; - removeSource(source, base); - return compileJoin(); - } - } else { - throw e; + watchErr = function(err) { + if (err.code !== 'ENOENT') { + throw err; + } + if (__indexOf.call(sources, source) < 0) { + return; + } + try { + rewatch(); + return compile(); + } catch (_error) { + removeSource(source, base); + return compileJoin(); } }; compile = function() { @@ -287,18 +286,18 @@ }); }); }; - try { - watcher = fs.watch(source, compile); - } catch (_error) { - e = _error; - watchErr(e); - } - return rewatch = function() { + rewatch = function() { if (watcher != null) { watcher.close(); } return watcher = fs.watch(source, compile); }; + try { + return watcher = fs.watch(source, compile); + } catch (_error) { + err = _error; + return watchErr(err); + } }; watchDir = function(source, base) { diff --git a/src/command.coffee b/src/command.coffee index dba07d9d..fc7bdfc3 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -186,43 +186,42 @@ compileJoin = -> # time the file is updated. May be used in combination with other options, # such as `--print`. watch = (source, base) -> - - prevStats = null + watcher = null + prevStats = null compileTimeout = null - watchErr = (e) -> - if e.code is 'ENOENT' - return if sources.indexOf(source) is -1 - try - rewatch() - compile() - catch e - removeSource source, base - compileJoin() - else throw e + watchErr = (err) -> + throw err unless err.code is 'ENOENT' + return unless source in sources + try + rewatch() + compile() + catch + removeSource source, base + compileJoin() compile = -> clearTimeout compileTimeout compileTimeout = wait 25, -> fs.stat source, (err, stats) -> return watchErr err if err - return rewatch() if prevStats and stats.size is prevStats.size and - stats.mtime.getTime() is prevStats.mtime.getTime() + return rewatch() if prevStats and + stats.size is prevStats.size and + stats.mtime.getTime() is prevStats.mtime.getTime() prevStats = stats fs.readFile source, (err, code) -> return watchErr err if err compileScript(source, code.toString(), base) rewatch() - try - watcher = fs.watch source, compile - catch e - watchErr e - rewatch = -> watcher?.close() watcher = fs.watch source, compile + try + watcher = fs.watch source, compile + catch err + watchErr err # Watch a directory of files for new additions. watchDir = (source, base) ->