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

57 lines
1.1 KiB
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-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.logger = options.logger;
2018-07-23 12:30:58 +00:00
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;
2018-08-08 12:42:45 +00:00
this.version = options.version;
this.ipc = options.ipc;
}
startConsole(){
2018-07-26 15:00:25 +00:00
this.console = new Console({
events: this.events,
plugins: this.plugins,
2018-08-08 12:42:45 +00:00
version: this.version,
ipc: this.ipc,
logger: this.logger
2018-07-26 15:00:25 +00:00
});
}
enhancedEval(cmd, context, filename, callback) {
2018-08-08 12:42:45 +00:00
this.console.executeCmd(cmd.trim(), callback);
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-08-08 12:42:45 +00:00
this.startConsole();
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;