Cakefile build task crashes on error

I've copied a few cake snippets from the internet and tried to automate the building of my project.

There are two versions of the build task. One uses exec:

{exec} = require 'child_process'
task 'build', 'Build project from src/*.coffee to lib/*.js', ->
  exec 'coffee --compile -m --output lib/client scripts/client/', (err, stdout, stderr) ->
    throw err if err
    console.log stdout + stderr

  exec 'coffee --compile -m --output lib/server scripts/server/', (err, stdout, stderr) ->
    throw err if err
    console.log stdout + stderr

and one uses spawn:

task "build2", "build and watch with spawn",->
    client = spawn "coffee", ["--compile", "--map", "--output", "lib/client", "scripts/client"]
    client.stdout.on "data", (data)->console.log data.toString().trim()

    server = spawn "coffee", ["--compile", "--map", "--output", "lib/server", "scripts/client"]
    server.stdout.on "data", (data)->console.log data.toString().trim()

I've manually compiled my project, started the server, checked that it worked and then executed the two tasks. They both returned without an error message.

Then I added one obviously wrong line to the client-side coffeescript, just something like "123/gff&&728709§""" and executed both tasks again:

  • The task with exec crashes with the error message "throw err, Command failed"
  • The task with spawn returned silently. It doesn't crash but neither does it record the error in my code

What is the right way to use cake for building coffeescript ? How can I fix my code ?