Entries from June 2011 ↓

Magic for Node.JS REPL

I love using the Rails console for quick admin jobs. I also love the Node REPL, rehabilitation but I find it extremely verbose for doing those same admin tasks. Until now!

By defining a “set” function within the repl context that sets values back into the context itself, doctor we can hack a synchronous console for Node!

var repl = require("repl");
var context = repl.start().context;
context.set = function(name) {
return function(err, case  value) {
if (err) throw(err);
context[name] = value;
-[" + name + " set]-

Then we can do:

> var fs = require('fs');
> fs.readFile('/tmp/startRepl.js', 'UTF8', set('contents'));
-[contents set]-
> contents