Skip to content

Commit

Permalink
Backporting the REPL fix for Node 0.3.7 and Coffee 1.0-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 1, 2011
1 parent 37308e6 commit a487259
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
21 changes: 13 additions & 8 deletions lib/repl.js
@@ -1,11 +1,12 @@
(function() {
var CoffeeScript, error, helpers, readline, repl, run, stdio;
var CoffeeScript, error, helpers, readline, repl, run, stdin, stdout;
CoffeeScript = require('./coffee-script');
helpers = require('./helpers');
readline = require('readline');
stdio = process.openStdin();
stdin = process.openStdin();
stdout = process.stdout;
error = function(err) {
return stdio.write((err.stack || err.toString()) + '\n\n');
return stdout.write((err.stack || err.toString()) + '\n\n');
};
helpers.extend(global, {
quit: function() {
Expand All @@ -29,13 +30,17 @@
return repl.prompt();
};
process.on('uncaughtException', error);
repl = readline.createInterface(stdio);
if (readline.createInterface.length < 3) {
repl = readline.createInterface(stdin);
stdin.on('data', function(buffer) {
return repl.write(buffer);
});
} else {
repl = readline.createInterface(stdin, stdout);
}
repl.setPrompt('coffee> ');
stdio.on('data', function(buffer) {
return repl.write(buffer);
});
repl.on('close', function() {
return stdio.destroy();
return stdin.destroy();
});
repl.on('line', run);
repl.prompt();
Expand Down
17 changes: 11 additions & 6 deletions src/repl.coffee
Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit a487259

Please sign in to comment.