Skip to content

Instantly share code, notes, and snippets.

@is8r
Created November 8, 2012 14:31
Show Gist options
  • Save is8r/4039117 to your computer and use it in GitHub Desktop.
Save is8r/4039117 to your computer and use it in GitHub Desktop.
Cakefile
#----------------------------------------------------------------------
#cake watch
#----------------------------------------------------------------------
#trg
src = 'htdocs/common/coffee/'
target = 'htdocs/common/js/main.js'
#source
{spawn} = require 'child_process'
fs = require 'fs'
util = require 'util'
task 'watch', '', (callback) ->
watch = spawn 'coffee', ['-w', "#{src}"]
watch.stderr.on 'data', (data) ->
process.stderr.write data.toString()
watch.stdout.on 'data', (data) ->
console.log 'file changed'
build = spawn 'coffee', ['-j', "#{target}", '-cl', "#{src}"]
build.stderr.on 'data', (data) ->
process.stderr.write data.toString()
build.on 'exit', (code) ->
if code is 0
console.log 'build complete'
#----------------------------------------------------------------------
#cake build
#----------------------------------------------------------------------
srcCoffeeDir = 'htdocs/common/coffee'
targetJsDir = 'htdocs/common/js'
coffeeFiles = [
'test.coffee'
]
#----------------------------------------------------------------------
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
targetFileName = 'main'
targetCoffeeFile = "#{srcCoffeeDir}/#{targetFileName}"
targetJsFile = "#{targetJsDir}/"
coffeeOpts = "--output #{targetJsFile} --compile #{targetCoffeeFile}"
task 'build', 'Build single JavaScript file from source files', ->
util.log "Building #{targetJsFile}"
appContents = new Array remaining = coffeeFiles.length
util.log "Appending #{coffeeFiles.length} files to #{targetCoffeeFile}"
for file, index in coffeeFiles then do (file, index) ->
fs.readFile "#{srcCoffeeDir}/#{file}"
, 'utf8'
, (err, fileContents) ->
util.log err if err
appContents[index] = fileContents
util.log "[#{index + 1}] #{file}"
process() if --remaining is 0 # (***)
process = ->
fs.writeFile targetCoffeeFile
, appContents.join('\n\n')
, 'utf8'
, (err) ->
util.log err if err
exec "coffee #{coffeeOpts}"
, (err, stdout, stderr) ->
util.log err if err
message = "Compiled #{targetJsFile}"
util.log message
fs.unlink targetCoffeeFile, (err) -> util.log err if err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment