     clearTimeout(timeout)
     timeout = setTimeout(done, timeoutInMillis)
 
-createEspruino = (port) ->
+createEspruino = (port, options) ->
   serialPort = new SerialPort(port, { baudrate: 9600 })
+  output = createOutput()
   commandExecuted = Q.defer()
 
-  serialPort: serialPort
+  finishCommand = createTimer(options.idleReadTimeBeforeClose, -> commandExecuted.resolve())
 
+  serialPort.on 'data', (data) ->
+    output.append(data.toString())
+    finishCommand()
+
+  log: -> output.all()
+  close: (callback) -> serialPort.close(callback)
   send: (command, onError) ->
+    serialPort.on('error', onError)
     commandExecuted = Q.defer()
     serialPort.write command, (error) -> onError(error) if error
     commandExecuted.promise
-
-  sendFinished: -> commandExecuted.resolve()
-
-  close: (callback) -> serialPort.close(callback)
 
 module.exports =
   deploy: (port, overrides = {}) ->

extend = require 'extend'
Q = require 'q'

output = (->
  consumed = ''

  append: (content) -> consumed += content
  all: -> consumed
)()

createPublisher = (readableStream, readableStreamDone) ->
  published = false

      reset: true
      save: true

    options = extend({}, defaults, overrides)
    espruino = createEspruino(port, options)

    onTransform = (chunk, encoding, callback) ->
      publish = createPublisher(@, callback)
      onError = (error) -> publish.error(error)

      Q().then(-> espruino.send("reset();\n", onError) if options.reset)
         .then(-> espruino.send("echo(0);\n", onError) if options.echoOff)
         .then(-> espruino.send("{ #{chunk.contents.toString()} }\n", onError))
         .then(-> espruino.send("echo(1);\n", onError) if options.echoOff)
         .then(-> espruino.send("save();\n", onError) if options.save)
         .then(-> publish.content(espruino.log()))

    onFlush = (callback) ->
      self = this
