Skip to content

Commit

Permalink
Clean up temp file on exit, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Sep 24, 2010
1 parent 6b5cb82 commit ff812e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 7 additions & 2 deletions Readme.md
Expand Up @@ -13,8 +13,10 @@ npm install repl-edit
Usage
=====

You can fire up a repl with editing capabilities by running `node-repl-edit`
or extend an existing repl session by typing `require('repl-edit').extend(global)`.
You can fire up a repl with editing capabilities by running `node-repl-edit`.

(It would be nice to extend an existing repl session but that's not possible with
Node's repl module right now.)


Commands
Expand Down Expand Up @@ -63,6 +65,9 @@ capability to provide commands like `.edit` and `.stash <filename>`.
The first time edit() is run in a repl instead of an empty file the command should be seeded
with the last command that was executed.

If the native repl module exports the currently running repl object it will be possible to attach
to an existing repl instead of having to run a separate binary that loads a repl.


License
=======
Expand Down
18 changes: 15 additions & 3 deletions lib/index.js
Expand Up @@ -5,17 +5,19 @@
// github.com/samsonjs/repl-edit
//

// TODO proper error handling
// TODO proper error handling, better intregration with node/lib/repl.js

var fs = require('fs')
, repl = require('repl')
, _repl

exports.startRepl = function() {
console.log('Commands: edit(), run(), stash(filename), unstash(filename), setEditor(editor)')
// TODO extend the repl context instead, problem is that repl.js:resetContext() isn't exported
// so simple assignments to _repl.context can't survive .clear yet
exports.extend(global)
_repl = require('repl').start()
_repl = repl.start()
return exports
}

exports.extend = function(obj) {
Expand Down Expand Up @@ -91,6 +93,16 @@ exports.extend = function(obj) {
sys.pump(read, fs.createWriteStream(_tmpfile))
})
}

process.on('exit', function() {
try {
fs.unlinkSync(_tmpfile)
} catch (e) {
// might not exist
}
})

return exports
}

function pausingRepl(fn) {
Expand All @@ -105,7 +117,7 @@ function pausingRepl(fn) {
_repl.displayPrompt()
})
}

function runFile(filename, callback) {
var Script = process.binding('evals').Script
, evalcx = Script.runInContext
Expand Down

0 comments on commit ff812e2

Please sign in to comment.