Skip to content

Instantly share code, notes, and snippets.

@danilopopeye
Forked from tbeseda/gist:1096141
Created November 2, 2011 00:20
Show Gist options
  • Select an option

  • Save danilopopeye/1332372 to your computer and use it in GitHub Desktop.

Select an option

Save danilopopeye/1332372 to your computer and use it in GitHub Desktop.
Cakefile to watch and recursively concat and compile CoffeeScript automatically.
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
appFiles = [
]
task 'count', 'how much coffee you got?!', ->
traverseFileSystem = (currentPath) ->
files = fs.readdirSync currentPath
for file in files
do (file) ->
currentFile = currentPath + '/' + file
stats = fs.statSync(currentFile)
if stats.isFile() and currentFile.indexOf('.coffee') > 1 and not ~appFiles.indexOf( currentFile )
appFiles.push currentFile
else if stats.isDirectory()
traverseFileSystem currentFile
traverseFileSystem 'src'
util.log "#{appFiles.length} coffee files found."
return appFiles
task 'watch', 'Watch prod source files and build changes', ->
invoke 'build'
util.log "Watching for changes in src"
for file in appFiles then do (file) ->
fs.watchFile file, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Whoa. Saw a change in #{file}. Building. Hold plz."
invoke 'build'
task 'build', 'Build single application file from source files', ->
invoke 'count'
appContents = new Array remaining = appFiles.length
for file, index in appFiles then do (file, index) ->
fs.readFile file, 'utf8', (err, fileContents) ->
throw err if err
appContents[index] = fileContents
process() if --remaining is 0
process = ->
fs.writeFile 'public/app.coffee', appContents.join('\n\n'), 'utf8', (err) ->
throw err if err
exec 'coffee --compile public/app.coffee', (err, stdout, stderr) ->
if err
error = stderr.substr(29).substr(0, stderr.indexOf('\n') - 29 )
util.log "Error compiling coffee file.\n\t#{error}"
else
fs.unlink 'public/app.coffee', (err) ->
if err
util.log 'Couldn\'t delete the app.coffee file/'
util.log 'Done building coffee file.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment