Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions lib/coffee-script/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 24 additions & 13 deletions src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,33 @@ loadRequires = ->
require req for req in opts.require
module.filename = realFilename

# 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 a source CoffeeScript file using `fs.watchFile` (or, if unsupported,
# `fs.watch`--see #1803), 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.stat source, (err, prevStats)->
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) ->
onChange = ->
fs.readFile source, (err, code) ->
throw err if err
compileScript(source, code.toString(), base)

if fs.watchFile
try
fs.watchFile source, {persistent: true, interval: 500}, (curr, prev) ->
onChange() unless curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
catch e
watchFileUnsupported = true

if watchFileUnsupported or !fs.watchFile
fs.watch source, (event) ->
if event is 'change'
fs.stat source, (err, stats) ->
throw err if err
compileScript(source, code.toString(), base)
return if stats.size is prevStats.size and
stats.mtime.getTime() is prevStats.mtime.getTime()
prevStats = stats
onChange()

# 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
Expand Down