mirror of
https://github.com/samsonjs/repl-edit.git
synced 2026-04-27 15:07:40 +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')
|
var fs = require('fs')
|
||||||
, spawn = require('child_process').spawn
|
, spawn = require('child_process').spawn
|
||||||
, util = require('util')
|
, util = require('util')
|
||||||
|
, Hint = 'Commands: .edit, .run, .stash <filename>, .unstash <filename>, .editor <editor>'
|
||||||
|
, repl
|
||||||
|
|
||||||
module.exports = { startRepl: startRepl, extendRepl: extendRepl }
|
module.exports = { startRepl: startRepl, extendRepl: extendRepl }
|
||||||
|
|
||||||
var repl
|
|
||||||
|
|
||||||
// Start a repl
|
// Start a repl
|
||||||
function startRepl() {
|
function startRepl() {
|
||||||
if (repl) {
|
if (repl) {
|
||||||
|
|
@ -24,6 +24,11 @@ function startRepl() {
|
||||||
extendRepl(require('repl').start())
|
extendRepl(require('repl').start())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function log(s) {
|
||||||
|
repl.outputStream.write(s + '\n' + repl.prompt)
|
||||||
|
repl.displayPrompt()
|
||||||
|
}
|
||||||
|
|
||||||
function extendRepl(theRepl) {
|
function extendRepl(theRepl) {
|
||||||
if (repl) {
|
if (repl) {
|
||||||
console.error('repl is already running, only one instance is allowed')
|
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')
|
log(Hint)
|
||||||
repl.displayPrompt()
|
|
||||||
|
|
||||||
repl.defineCommand('edit', {
|
repl.defineCommand('edit', {
|
||||||
help: 'Edit the current command in your text editor',
|
help: 'Edit the current command in your text editor',
|
||||||
|
|
@ -113,15 +117,14 @@ function stash(cmdFile, dest) {
|
||||||
try {
|
try {
|
||||||
fs.statSync(cmdFile)
|
fs.statSync(cmdFile)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('nothing to stash')
|
log('nothing to stash')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var read = fs.createReadStream(cmdFile)
|
var read = fs.createReadStream(cmdFile)
|
||||||
read.on('end', function() {
|
read.on('end', function() {
|
||||||
console.log('stashed')
|
log('stashed')
|
||||||
unpause()
|
unpause()
|
||||||
})
|
})
|
||||||
// TODO confirm before overwriting an existing file
|
|
||||||
pause()
|
pause()
|
||||||
util.pump(read, fs.createWriteStream(dest))
|
util.pump(read, fs.createWriteStream(dest))
|
||||||
}
|
}
|
||||||
|
|
@ -130,12 +133,12 @@ function unstash(cmdFile, source) {
|
||||||
try {
|
try {
|
||||||
fs.statSync(source)
|
fs.statSync(source)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('no stash at ' + source)
|
log('no stash at ' + source)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var read = fs.createReadStream(source)
|
var read = fs.createReadStream(source)
|
||||||
read.on('end', function() {
|
read.on('end', function() {
|
||||||
console.log('unstashed')
|
log('unstashed')
|
||||||
unpause()
|
unpause()
|
||||||
})
|
})
|
||||||
pause()
|
pause()
|
||||||
|
|
@ -147,7 +150,7 @@ function run(filename, callback) {
|
||||||
try {
|
try {
|
||||||
fs.statSync(filename)
|
fs.statSync(filename)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
repl.stream.write('nothing to run\n')
|
log('nothing to run\n')
|
||||||
callback()
|
callback()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -168,9 +171,9 @@ function run(filename, callback) {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// On error: Print the error and clear the buffer
|
// On error: Print the error and clear the buffer
|
||||||
if (e.stack) {
|
if (e.stack) {
|
||||||
repl.stream.write(e.stack + "\n");
|
log(e.stack + "\n");
|
||||||
} else {
|
} else {
|
||||||
repl.stream.write(e.toString() + "\n");
|
log(e.toString() + "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (callback) callback()
|
if (callback) callback()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue