Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Forked from dshaw/replify.js
Created July 13, 2012 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianloveswords/3105780 to your computer and use it in GitHub Desktop.
Save brianloveswords/3105780 to your computer and use it in GitHub Desktop.
Add a repl to Node v0.6 or v0.8
var fs = require("fs")
, net = require("net")
, repl = require("repl")
module.exports = function replify (name, ctx) {
var repl_path = "/tmp/" + name + ".sock"
ctx || (ctx = {})
fs.unlink(repl_path, function () {
// intentionally not listening for the error. either way, we're good.
net.createServer(function repl_onrequest(socket) {
var repl_opt = {
prompt: name + "> "
, input: socket
, output: socket
, terminal: true
, useGlobal: false
},
r = null
socket.columns = 132 // Set screen width for autocomplete. You can modify this locally in your repl.
if (fs.exists) { // if `fs.exists` exists, then it's node v0.8
r = repl.start(repl_opt)
r.on('exit', function () {
socket.end()
})
} else {
r = repl.start(repl_opt.prompt, socket)
}
r.context.socket = socket
Object.keys(ctx).forEach(function (key) {
// don't pave me, bro!
if (!r.context[key]) r.context[key] = ctx[key]
})
}).listen(repl_path)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment