less-watcher.coffee | |
|---|---|
| less-watcher is a script that can watch a directory and recompile your .less styles if they change. It's very useful for development as you don't need to think about recompiling your Less files. Search is done in a recursive manner so sub-directories are handled as well.
Installing less-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:
less-watcher requires: | |
| Specify the command line arguments for the script (using optimist) | usage = "Watch a directory and recompile .less styles if they change.\nUsage: less-watcher -p [prefix] -d [directory]."
specs = require('optimist')
.usage(usage)
.default('d', '.')
.describe('d', 'Specify which directory to scan.')
.default('p', '.less.')
.describe('p', 'Which prefix should the compiled files have? Default is style.less will be compiled to .less.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 *.less files using | findLessFiles = (dir) ->
watcher_lib.findFiles('*.less', dir, compileIfNeeded) |
| Keeps a track of modified times for .less files in a in-memory object, if a .less file is modified it recompiles it using compileLessScript. When starting the script all files will be recompiled. | WATCHED_FILES = {}
compileIfNeeded = (file) ->
watcher_lib.compileIfNeeded(WATCHED_FILES, file, compileLessScript) |
| Compiles a file using | compileLessScript = (file) ->
fnGetOutputFile = (file) -> file.replace(/([^\/\\]+)\.less/, "#{argv.p}$1.css")
watcher_lib.compileFile("lessc #{ 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, findLessFiles)
|