Created
July 12, 2012 00:57
-
-
Save dshaw/3094870 to your computer and use it in GitHub Desktop.
Add a repl to Node v0.6 or v0.8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
} |
You would almost create a module just for this inconsistent behavior
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Key considerations: