Skip to content

Instantly share code, notes, and snippets.

@marconi
Created October 28, 2012 09:02
Show Gist options
  • Save marconi/3968102 to your computer and use it in GitHub Desktop.
Save marconi/3968102 to your computer and use it in GitHub Desktop.
Cake file that watches coffee files and compiles them.
###
Compiles coffee scripts from coffee/ to js/.
###
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
path = require 'path'
appFiles = []
task 'coffeeFiles', 'collect coffee files', ->
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 currentFile not in appFiles
appFiles.push currentFile
else if stats.isDirectory()
traverseFileSystem currentFile
traverseFileSystem 'coffee'
util.log "#{appFiles.length} coffee files found."
return appFiles
task 'watch', 'watch coffee files and build changes', ->
invoke 'coffeeFiles'
util.log "watching for changes..."
for file in appFiles then do (file) ->
fs.watchFile file, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "saw change in #{file}"
outputPath = path.dirname(file).replace /coffee/, "js"
exec "coffee -cwo #{outputPath} #{file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment