From a26091276697c6229289f5d7b08c3759d6a63d12 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 18 May 2011 22:08:18 -0700 Subject: [PATCH] fix output still using the 0.2.x repl stream --- lib/index.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index c7ce908..9b92e1f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,11 +10,11 @@ var fs = require('fs') , spawn = require('child_process').spawn , util = require('util') + , Hint = 'Commands: .edit, .run, .stash , .unstash , .editor ' + , repl module.exports = { startRepl: startRepl, extendRepl: extendRepl } -var repl - // Start a repl function startRepl() { if (repl) { @@ -24,6 +24,11 @@ function startRepl() { extendRepl(require('repl').start()) } +function log(s) { + repl.outputStream.write(s + '\n' + repl.prompt) + repl.displayPrompt() +} + function extendRepl(theRepl) { if (repl) { console.error('repl is already running, only one instance is allowed') @@ -46,8 +51,7 @@ function extendRepl(theRepl) { } }) - repl.outputStream.write('Commands: .edit, .run, .stash , .unstash , .editor \n') - repl.displayPrompt() + log(Hint) repl.defineCommand('edit', { help: 'Edit the current command in your text editor', @@ -113,15 +117,14 @@ function stash(cmdFile, dest) { try { fs.statSync(cmdFile) } catch (e) { - console.log('nothing to stash') + log('nothing to stash') return } var read = fs.createReadStream(cmdFile) read.on('end', function() { - console.log('stashed') + log('stashed') unpause() }) - // TODO confirm before overwriting an existing file pause() util.pump(read, fs.createWriteStream(dest)) } @@ -130,12 +133,12 @@ function unstash(cmdFile, source) { try { fs.statSync(source) } catch (e) { - console.log('no stash at ' + source) + log('no stash at ' + source) return } var read = fs.createReadStream(source) read.on('end', function() { - console.log('unstashed') + log('unstashed') unpause() }) pause() @@ -147,7 +150,7 @@ function run(filename, callback) { try { fs.statSync(filename) } catch (e) { - repl.stream.write('nothing to run\n') + log('nothing to run\n') callback() return } @@ -168,9 +171,9 @@ function run(filename, callback) { } catch (e) { // On error: Print the error and clear the buffer if (e.stack) { - repl.stream.write(e.stack + "\n"); + log(e.stack + "\n"); } else { - repl.stream.write(e.toString() + "\n"); + log(e.toString() + "\n"); } } if (callback) callback()