coffee-watcher.coffee | |
|---|---|
| coffee-watcher is a script that can watch a directory and recompile your .coffee scripts if they change. It's very useful for development as you don't need to think about recompiling your CoffeeScript files. Search is done in a recursive manner so sub-directories are handled as well.
Installing coffee-watcher is easy with npm:
Run this to watch for changes in the current working directory:
Run this to watch for changes in a specified directory:
coffee-watcher requires: | |
| Specify the command line arguments for the script (using optimist) | usage = "Watch a directory and recompile .coffee scripts if they change.\nUsage: coffee-watcher -p [prefix] -d [directory]."
specs = require('optimist')
.usage(usage)
.default('d', '.')
.describe('d', 'Specify which directory to scan.')
.default('p', '.coffee.')
.describe('p', 'Which prefix should the compiled files have? Default is script.coffee will be compiled to .coffee.style.css.')
.boolean('h')
.describe('h', 'Prints help') |
| Handle the special -h case | if specs.parse(process.argv).h
specs.showHelp()
process.exit()
else
argv = specs.argv |
| Use | watcher_lib = require 'watcher_lib' |
| Searches through a directory structure for *.coffee files using | findCoffeeFiles = (dir) ->
watcher_lib.findFiles('*.coffee', dir, compileIfNeeded) |
| Keeps a track of modified times for .coffee files in a in-memory object, if a .coffee file is modified it recompiles it using compileCoffeeScript. When starting the script all files will be recompiled. | WATCHED_FILES = {}
compileIfNeeded = (file) ->
watcher_lib.compileIfNeeded(WATCHED_FILES, file, compileCoffeeScript) |
| Compiles a file using | compileCoffeeScript = (file) ->
fnGetOutputFile = (file) -> file.replace(/([^\/\\]+)\.coffee/, "#{argv.p}$1.js")
watcher_lib.compileFile("coffee -bp #{ file }", file, fnGetOutputFile) |
| Starts a poller that polls each second in a directory that's either by default the current working directory or a directory that's passed through process arguments. | watcher_lib.startDirectoryPoll(argv.d, findCoffeeFiles)
|