embark-area-51/cmd/dashboard/repl.js

44 lines
975 B
JavaScript
Raw Normal View History

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-09-13 14:28:03 +00:00
2018-07-23 12:30:58 +00:00
class REPL {
constructor(options) {
2018-07-26 15:00:25 +00:00
this.events = options.events;
2018-09-13 14:28:03 +00:00
this.env = options.env;
2018-07-26 15:00:25 +00:00
}
enhancedEval(cmd, context, filename, callback) {
2018-09-13 14:28:03 +00:00
this.events.request('console:executeCmd', cmd.trim(), function (err, message) {
callback(err, message || ''); // This way, we don't print undefined
});
2018-07-23 12:30:58 +00:00
}
2018-07-26 15:56:50 +00:00
enhancedWriter(output) {
if ((typeof output) === "string") {
return output;
}
2018-09-13 14:28:03 +00:00
return util.inspect(output, {colors: true});
2018-07-26 15:56:50 +00:00
}
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-08-24 13:05:19 +00:00
this.events.request("runcode:getContext", (context) => {
this.replServer.context = context;
});
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();
}
}
module.exports = REPL;