fs = require "fs"
{exec} = require "child_process"

appFiles = [
  "funddy"
  "cross_domain"
]

task "build", "Build cross domain file", (options) ->
  output = "funddycrossdomain"
  appContents = new Array remaining = appFiles.length

  for file, index in appFiles then do (file, index) ->
    fs.readFile "src/#{file}.coffee", 'utf8', (err, fileContents) ->
      throw err if err
      appContents[index] = fileContents
      process() if --remaining is 0

  process = ->
    fs.writeFile "lib/#{output}.coffee", appContents.join("\n\n"), "utf8", (err) ->
      throw err if err
      exec "coffee --compile lib/#{output}.coffee", (err, stdout, stderr) ->
        throw err if err
        console.log stdout + stderr
        fs.unlink "lib/#{output}.coffee", (err) ->
          throw err if err
          console.log "Done"