mirror of
https://github.com/samsonjs/repl-edit.git
synced 2026-04-08 11:45:56 +00:00
fix output still using the 0.2.x repl stream
This commit is contained in:
parent
89cce91212
commit
a260912766
1 changed files with 15 additions and 12 deletions
27
lib/index.js
27
lib/index.js
|
|
@ -10,11 +10,11 @@
|
|||
var fs = require('fs')
|
||||
, spawn = require('child_process').spawn
|
||||
, util = require('util')
|
||||
, Hint = 'Commands: .edit, .run, .stash <filename>, .unstash <filename>, .editor <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 <filename>, .unstash <filename>, .editor <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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue