repl.js
1 const repl = require("repl"); 2 const util = require("util"); 3 let fs = require('../../lib/core/fs'); 4 5 class REPL { 6 constructor(options) { 7 this.events = options.events; 8 this.env = options.env; 9 } 10 11 enhancedEval(cmd, context, filename, callback) { 12 this.events.request('console:executeCmd', cmd.trim(), function (err, message) { 13 callback(err, message || ''); // This way, we don't print undefined 14 }); 15 } 16 17 enhancedWriter(output) { 18 if ((typeof output) === "string") { 19 return output; 20 } 21 return util.inspect(output, {colors: true}); 22 } 23 24 start(done) { 25 this.replServer = repl.start({ 26 prompt: "Embark (" + this.env + ") > ", 27 useGlobal: true, 28 eval: this.enhancedEval.bind(this), 29 writer: this.enhancedWriter.bind(this) 30 }); 31 32 this.events.request('console:history', (err, history) => { 33 history 34 .split('\n') 35 .forEach((cmd) => { this.replServer.history.push(cmd); }); 36 }); 37 38 this.events.request("runcode:getContext", (context) => { 39 this.replServer.context = context; 40 }); 41 42 this.replServer.on("exit", () => { 43 process.exit(); 44 }); 45 46 done(); 47 } 48 } 49 50 module.exports = REPL;