Skip to content

Instantly share code, notes, and snippets.

@jrmoran
Created December 30, 2011 01:44
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jrmoran/1537200 to your computer and use it in GitHub Desktop.
Save jrmoran/1537200 to your computer and use it in GitHub Desktop.
Cakefile to document, compile, join and minify CoffeeScript files for client side apps.
# Cakefile to document, compile, join and minify CoffeeScript files for
# client side apps. Just edit the config object literal.
#
# -jrmoran
fs = require 'fs'
{exec, spawn} = require 'child_process'
# order of files in `inFiles` is important
config =
srcDir: 'coffee'
outDir: 'js'
inFiles: [ 'color', 'imageCanvas', 'app' ]
outFile: 'client'
yuic: '~/Dropbox/toolbox/dotfiles/bin/yuicompressor-2.4.2.jar'
outJS = "#{config.outDir}/#{config.outFile}"
strFiles = ("#{config.srcDir}/#{file}.coffee" for file in config.inFiles).join ' '
# deal with errors from child processes
exerr = (err, sout, serr)->
process.stdout.write err if err
process.stdout.write sout if sout
process.stdout.write serr if serr
task 'doc', 'generate documentation for *.coffee files', ->
exec "docco #{config.srcDir}/*.coffee", exerr
# this will keep the non-minified compiled and joined file updated as files in
# `inFile` change.
task 'watch', 'watch and compile changes in source dir', ->
watch = exec "coffee -j #{outJS}.js -cw #{strFiles}"
watch.stdout.on 'data', (data)-> process.stdout.write data
task 'build', 'join and compile *.coffee files', ->
exec "coffee -j #{outJS}.js -c #{strFiles}", exerr
task 'min', 'minify compiled *.js file', ->
exec "java -jar #{config.yuic} #{outJS}.js -o #{outJS}.min.js", exerr
task 'bam', 'build and minify', ->
invoke 'build'
invoke 'min'
task 'test', 'runs jasmine tests', ->
exec 'jasmine-node --coffee --verbose spec', exerr
# watch files and run tests automatically
task 'watch:test', 'watch and run tests', ->
console.log 'watching...'
whenChanged = (filename, fun)->
fs.watchFile filename, (curr, prev)->
fun() if curr.mtime > prev.mtime
for f in config.inFiles
whenChanged "#{f}.coffee", ->
console.log "===== TEST #{new Date().toLocaleString()} ====="
invoke 'test'
@EinLama
Copy link

EinLama commented Apr 16, 2012

This is a nice piece of work :) Thank you, I'm basing my first Cakefile on this one.

@Kadrian
Copy link

Kadrian commented Jul 2, 2013

Thank you! I'm using this with a bash script to watch my current directory for changes:

while true; do
    new=$(find "coffee/" \
        -not \( -type f -name ".?*" -prune \) \
        -print0 2>/dev/null | xargs -0 stat -f "%m %z %N")

    if [ "$new" != "$old" ]; then
        cake bam
        old=$new
    fi

    sleep 1
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment