Search code examples
javascriptcoffeescript

Compiling CoffeeScript with a Cakefile


Currently i use this to compile it to a file:

task 'CurrentVillage', 'Build Current Vilage', ->
  remaining = appFiles.length
  appContents = new Array remaining  
  for file, index in appFiles then do (file, index) ->
    fs.readFile "media/coffee/#{file}.coffee", 'utf8', (err, fileContents) ->
      throw err if err
      appContents[index] = fileContents
      process() if --remaining is 0
  process = ->
    fs.writeFile 'media/coffee/frontend/VillageCanvas.coffee', appContents.join('\n\n'), 'utf8', (err) ->
      throw err if err

I dont get it to compile it directly to javascript :S


Solution

  • You need to define your tasks in a Cakefile, then invoke that Cakefile. Run cake build in your terminal from the directory where your coffeescript files reside, after putting a Cakefile in the same directory. Here's a simple template for a Cakefile. It already has the build function written in as described below: http://twilson63.github.com/cakefile-template/

    build = (watch, callback) ->
      if typeof watch is 'function'
        callback = watch
        watch = false
    
      options = ['-c', '-b', '-o', 'lib', 'src']
      options.unshift '-w' if watch
      launch 'coffee', options, callback