Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple Cakefile for LESS + CoffeeScript with file watching
{exec} = require 'child_process'
fs = require 'fs'
lastChange = {}
scripts = ['scripts/']
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 file, (event, filename) ->
return if event isnt 'change'
# ignore repeated event misfires
fn file if - lastChange[file] > 1000
lastChange[file] =
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
You can’t perform that action at this time.