Skip to content

Instantly share code, notes, and snippets.

@tbeseda
Created July 20, 2011 23:05
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbeseda/1096141 to your computer and use it in GitHub Desktop.
Save tbeseda/1096141 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'
growl = require 'growl'
appFiles = [
'src/hasToGoFirst.coffee' # put your files that must be loaded in first into the array
'src/notAsImportantButNeedsToGoBeforeTheRest.coffee'
]
task 'coffeeFiles', '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 appFiles.join('=').indexOf("#{currentFile}=") < 0
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 "Saw change in #{file}"
grrrr 'Whoa. Saw a change. Building. Hold plz.'
invoke 'build'
task 'build', 'Build single application file from source files', ->
invoke 'coffeeFiles'
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
util.log 'Error compiling coffee file.'
grrrr 'Uh, your coffee is bad.'
else
fs.unlink 'public/app.coffee', (err) ->
if err
util.log 'Couldn\'t delete the app.coffee file/'
util.log 'Done building coffee file.'
grrrr 'Okay, coffee is ready.'
grrrr = (message = '') ->
options =
title: 'CoffeeScript'
growl.notify message, options
@tbeseda
Copy link
Author

tbeseda commented Jul 20, 2011

Borrowing a lot from CoffeeScript wiki, but instead of manually specifying an array of files, this script automatically looks for .coffee files. Don't worry, there is a list of files that can be used to prioritize files.

This looks at your src directory recursively for .coffee files, combines them, compiles them to a single app.js file in public/.

Includes (growlnotify)[http://growl.info/extras.php](Grow CLI) hook.
Also, credit to (Hovhannes Avoyan)[http://blog.monitis.com/index.php/2011/07/09/6-node-js-recipes-working-with-the-file-system/] for the file traversal method.

@BernhardPosselt
Copy link

Could you license this under some license :) id be grateful

@enyo
Copy link

enyo commented Feb 28, 2013

This gist is pretty outdated. It uses the deprecated watchFile API and an outdated growl.notify call.

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