Last active
December 24, 2015 15:18
-
-
Save jasoncrawford/6818650 to your computer and use it in GitHub Desktop.
Node REPL that doesn't clobber _
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 repl = require('repl') | |
var vm = require('vm'); | |
var _; | |
var server = repl.start({ | |
eval: function (cmd, context, filename, callback) { | |
try { | |
var match = cmd.match(/^\((.*)\n\)$/); | |
var code = match ? match[1] : cmd; | |
context._ = _; | |
var result = vm.runInThisContext(code, filename); | |
} catch (error) { | |
console.log(error.stack); | |
} finally { | |
_ = context._; | |
callback(null, result); | |
} | |
} | |
}).on('exit', function () { | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note one deficiency relative to just running
node
: You can't enter code that spans more than one line. You'll get a syntax error. Haven't figured that one out yet.