Created
May 21, 2012 20:48
-
-
Save ricardobeat/2764583 to your computer and use it in GitHub Desktop.
Simple Cakefile for LESS + CoffeeScript with file watching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{exec} = require 'child_process' | |
fs = require 'fs' | |
lastChange = {} | |
scripts = ['scripts/game.coffee'] | |
styles = ['styles/game.less'] | |
compileCoffee = (file) -> | |
exec "coffee -c #{file}", (err, stdout, stderr) -> | |
return console.error err if err | |
console.log "Compiled #{file}" | |
compileLess = (file) -> | |
exec "lessc #{file} #{file.replace('.less', '.css')}", (err, stdout, err) -> | |
return console.error err if err | |
console.log "Compiled #{file}" | |
watchFile = (file, fn) -> | |
try | |
fs.watch file, (event, filename) -> | |
return if event isnt 'change' | |
# ignore repeated event misfires | |
fn file if Date.now() - lastChange[file] > 1000 | |
lastChange[file] = Date.now() | |
catch e | |
console.log "Error watching #{file}" | |
watchFiles = (files, fn) -> | |
for file in files | |
lastChange[file] = 0 | |
watchFile file, fn | |
console.log "Watching #{file}" | |
task 'build', 'Compile *.coffee and *.less', -> | |
compileCoffee(f) for f in scripts | |
compileLess(f) for f in styles | |
task 'watch', 'Compile + watch *.coffee and *.less', -> | |
watchFiles scripts, compileCoffee | |
watchFiles styles, compileLess | |
task 'watch:js', 'Compile + watch *.coffee only', -> | |
watchFiles scripts, compileCoffee | |
task 'watch:css', 'Compile + watch *.less only', -> | |
watchFiles styles, compileLess |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment