Skip to content

Instantly share code, notes, and snippets.

@jocafa
Created May 11, 2011 19:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jocafa/967083 to your computer and use it in GitHub Desktop.
Save jocafa/967083 to your computer and use it in GitHub Desktop.
A Cakefile
sys = require 'sys'
fs = require 'fs'
util = require 'util'
childProc = require 'child_process'
coffee = require 'coffee-script'
stylus = require 'stylus'
uglify = require 'uglify-js'
_ = require 'underscore'
_.mixin(require('underscore.string'))
timestamps = {}
glowProc = null
recursivelyCat = (path, ext) ->
files = []
dirs = []
flat = ""
for thing in fs.readdirSync(path).sort()
stats = fs.statSync("#{path}/#{thing}")
if stats.isFile()
if _(thing).endsWith(".#{ext}")
files.push thing
else if stats.isDirectory()
dirs.push thing
for dir in dirs
flat += arguments.callee("#{path}/#{dir}/", ext)
for file in files
flat += '\n\n' + fs.readFileSync("#{path}/#{file}", 'utf8')
flat
detectChanges = (path) ->
changesFound = false
for thing in fs.readdirSync(path).sort()
if !_(thing).includes('Build')
fullthing = "#{path}/#{thing}"
stats = fs.statSync(fullthing)
if stats.isFile()
if (timestamps[fullthing] + '') != (stats.mtime + '')
changesFound = true
timestamps[fullthing] = stats.mtime
else if stats.isDirectory()
changesFound = arguments.callee(fullthing) || changesFound
return changesFound
task 'build', 'Build all the static files', ->
console.log 'building coffeeBuild.js'
coffeeSrc = recursivelyCat('./static/', 'coffee')
coffeeBuild = coffee.compile coffeeSrc
fs.writeFileSync './static/coffeeBuild.js', coffeeBuild
console.log 'building coffeeBuild.min.js'
parsed = uglify.parser.parse(coffeeBuild)
mangled = uglify.uglify.ast_mangle(parsed)
squeezed = uglify.uglify.ast_squeeze(mangled)
generated = uglify.uglify.gen_code(squeezed)
fs.writeFileSync './static/coffeeBuild.min.js', generated
console.log 'building stylusBuild.css'
stylusSrc = recursivelyCat('./static/', 'styl')
stylus(stylusSrc).render (err, css) ->
fs.writeFileSync './static/stylusBuild.css', css
console.log 'building stylusBuild.min.css'
stylus(stylusSrc, {compress: true}).render (err, css) ->
fs.writeFileSync './static/stylusBuild.min.css', css
console.log 'all done'
task 'run', 'Run coffee glow.coffee', ->
if glowProc
console.log 'killing glow'
glowProc.kill()
console.log 'starting glow'
glowProc = childProc.exec 'coffee glow.coffee'
glowProc.stdout.on 'data', (data) ->
sys.print data
glowProc.stderr.on 'data', (data) ->
sys.print data
glowProc.stderr.on 'exit', (code) ->
sys.print "glow exited: #{code}"
task 'watch', 'Watch me code and recompile stuff as needed', ->
invoke 'build'
detectChanges('.')
invoke 'run'
watchfn = ->
if detectChanges('.') is true
invoke 'build'
invoke 'run'
setTimeout arguments.callee, 1000
watchfn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment