Quick Fun with CoffeeScript File Watching

I was spinning up a new project using CoffeeScript today and had to remind myself of the command flags. That's when I noticed the wording of the -w documentation:

-w --watch    watch scripts for changes and rerun commands

Now I've used -w to recompile scripts as I edit them plenty of times, but the wording suggested a possibility I hadn't considered: it works for any command, not just compilation!

How is this useful? For one, this allows us to use the coffee command as a kind of "super REPL". Let's say I'm tinkering around with an idea that requires a bit more setup than the one-liners normally allowed in a REPL. I can dump it into a file:

# sandbox.coffee

x = "First"
y = "Second"

console.log "Is it the #{x} or #{y}"

And run it:

coffee -w sandbox.coffee sandbox.coffee

Now each time I save sandbox.coffee the script will be run again, allowing me to tinker and modify while still getting immediate results. Neat!