Backporting the REPL fix for Node 0.3.7 and Coffee 1.0-stable

This commit is contained in:
Jeremy Ashkenas
2011-01-31 22:27:56 -05:00
parent 37308e6760
commit a487259e8e
2 changed files with 24 additions and 14 deletions

View File

@@ -9,12 +9,13 @@ CoffeeScript = require './coffee-script'
helpers = require './helpers'
readline = require 'readline'
# Start by opening up **stdio**.
stdio = process.openStdin()
# Start by opening up `stdin` and `stdout`.
stdin = process.openStdin()
stdout = process.stdout
# Log an error.
error = (err) ->
stdio.write (err.stack or err.toString()) + '\n\n'
stdout.write (err.stack or err.toString()) + '\n\n'
# Quick alias for quitting the REPL.
helpers.extend global, quit: -> process.exit(0)
@@ -34,9 +35,13 @@ run = (buffer) ->
process.on 'uncaughtException', error
# Create the REPL by listening to **stdin**.
repl = readline.createInterface stdio
if readline.createInterface.length < 3
repl = readline.createInterface stdin
stdin.on 'data', (buffer) -> repl.write buffer
else
repl = readline.createInterface stdin, stdout
repl.setPrompt 'coffee> '
stdio.on 'data', (buffer) -> repl.write buffer
repl.on 'close', -> stdio.destroy()
repl.on 'close', -> stdin.destroy()
repl.on 'line', run
repl.prompt()