From b2e8f57d24af32d3f82c81057beaba380a4cf896 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Sat, 25 Apr 2015 19:00:36 -0700 Subject: [PATCH] Add a basic class wrapping Node's repl --- lib/repl.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/repl.js diff --git a/lib/repl.js b/lib/repl.js new file mode 100644 index 0000000..1d2ef96 --- /dev/null +++ b/lib/repl.js @@ -0,0 +1,55 @@ +"use strict"; + +var util = require("util"), + createRepl = require("repl").start, + EventEmitter = require("events").EventEmitter; + +var _ = require("./utils/helpers"); + +// asserts that a constructor was called with 'new' +function classCallCheck(instance, constructor) { + if (!instance instanceof constructor) { + throw new TypeError("Cannot call a class as a function"); + } +} + +var Repl = module.exports = function Repl(opts, context) { + classCallCheck(this, Repl); + + opts = opts || {}; + context = context || {}; + + opts.prompt = opts.prompt || "repl > "; + opts.stdin = opts.stdin || process.stdin; + opts.stdout = opts.stdout || process.stdout; + + this.repl = null; + this.options = opts; + this.context = context; +}; + +Repl.active = false; + +util.inherits(Repl, EventEmitter); + +Repl.prototype.start = function() { + // don't try to start two repls at once + if (Repl.active) { + return false; + } + + Repl.active = true; + + this.repl = createRepl(this.options); + _.extend(this.repl.context, this.context); + + this.repl.on("exit", function() { + Repl.active = false; + this.emit("exit"); + }.bind(this)); + + this.repl.on("reset", function(context) { + _.extend(context, this.context); + this.emit("reset"); + }.bind(this)); +};