2018-07-23 12:30:58 +00:00
|
|
|
const repl = require("repl");
|
2018-07-26 15:56:50 +00:00
|
|
|
const util = require("util");
|
2018-07-23 12:30:58 +00:00
|
|
|
|
2018-07-26 15:00:25 +00:00
|
|
|
const Console = require('./console.js');
|
|
|
|
|
2018-07-23 12:30:58 +00:00
|
|
|
class REPL {
|
|
|
|
constructor(options) {
|
|
|
|
this.env = options.env;
|
2018-07-25 09:15:43 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-07-26 15:00:25 +00:00
|
|
|
this.events = options.events;
|
|
|
|
this.console = new Console({
|
|
|
|
events: this.events,
|
|
|
|
plugins: this.plugins,
|
|
|
|
version: options.version
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
enhancedEval(cmd, context, filename, callback) {
|
|
|
|
this.console.executeCmd(cmd.trim(), (result) => {
|
|
|
|
callback(null, result);
|
|
|
|
});
|
2018-07-23 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 15:56:50 +00:00
|
|
|
enhancedWriter(output) {
|
|
|
|
if ((typeof output) === "string") {
|
|
|
|
return output;
|
|
|
|
} else {
|
|
|
|
return util.inspect(output, {colors: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:30:58 +00:00
|
|
|
start(done) {
|
2018-07-23 16:05:01 +00:00
|
|
|
this.replServer = repl.start({
|
2018-07-24 08:40:48 +00:00
|
|
|
prompt: "Embark (" + this.env + ") > ",
|
2018-07-26 15:00:25 +00:00
|
|
|
useGlobal: true,
|
2018-07-26 15:56:50 +00:00
|
|
|
eval: this.enhancedEval.bind(this),
|
|
|
|
writer: this.enhancedWriter.bind(this)
|
2018-07-23 12:30:58 +00:00
|
|
|
});
|
|
|
|
|
2018-07-23 16:05:01 +00:00
|
|
|
this.replServer.on("exit", () => {
|
2018-07-23 12:41:13 +00:00
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
|
2018-07-23 12:30:58 +00:00
|
|
|
done();
|
|
|
|
}
|
2018-07-23 16:05:01 +00:00
|
|
|
|
2018-07-23 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = REPL;
|