Skip to content

Instantly share code, notes, and snippets.

@ricardobeat
Created May 21, 2012 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ricardobeat/2764583 to your computer and use it in GitHub Desktop.
Save ricardobeat/2764583 to your computer and use it in GitHub Desktop.
Simple Cakefile for LESS + CoffeeScript with file watching
{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