Skip to content

Commit

Permalink
feat(@embark/cli): repl support inside dashboard
Browse files Browse the repository at this point in the history
Closes #768
  • Loading branch information
lastmjs authored and 0x-r4bbit committed Nov 22, 2018
1 parent 6526e83 commit 382fbc4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 103 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"subdir": "0.0.3",
"swarm-api": "0.1.2",
"tar": "3.2.1",
"term.js": "0.0.7",
"toposort": "1.0.7",
"underscore": "1.9.1",
"url-loader": "1.1.2",
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Dashboard {
this.events.on('status', monitor.setStatus.bind(monitor));
this.events.on('servicesState', monitor.availableServices.bind(monitor));

this.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor));
//TODO Should we setup a handler for logger:log or something similar?
//TODO So everywhere that we call logText.this.logger, we can just raise an event?

this.logger.info('========================'.bold.green);
this.logger.info((__('Welcome to Embark') + ' ' + this.version).yellow.bold);
Expand Down
188 changes: 94 additions & 94 deletions src/cmd/dashboard/monitor.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
let blessed = require("neo-blessed");
let CommandHistory = require('./command_history.js');
const REPL = require('./repl.js');
const stream = require('stream');

class Monitor {
constructor(_options) {
let options = _options || {};
this.env = options.env;
this.console = options.console;
this.history = new CommandHistory();
this.events = options.events;

this.color = options.color || "green";
this.minimal = options.minimal || false;

Expand All @@ -23,7 +22,7 @@ class Monitor {
this.layoutLog();
this.layoutStatus();
this.layoutModules();
this.layoutCmd();
this.layoutTerminal();

this.screen.key(["C-c"], function () {
process.exit(0);
Expand All @@ -36,7 +35,64 @@ class Monitor {
this.status.setContent(this.env.green);

this.screen.render();
this.input.focus();

this.terminalReadableStream = new stream.Readable({
read() {}
});

const logText = this.logText;
const terminal = this.terminal;
const terminalWritableStream = new stream.Writable({
write(chunk, encoding, next) {
// const terminalPrompt = "Embark (" + this.env + ") > ";
// const chunkString = chunk.toString();

// if (chunkString.contains(terminalPrompt)) {
// // terminal.write()
// }
// else {

// }

// const regex = new RegExp(`(.*)(${terminalPrompt})`);
// const groups = regex.exec(chunkString);

// if (groups === null) {
// terminal.write(chunk.toString());
// }
// else {
// logText.log(groups[1]);
// terminal.write(groups[2]);
// }

terminal.write(chunk.toString());

next();
}
});

// process.stderr.on('data', (data) => {
// this.logText.log(data.toString());
// // process.exit(0);
// });

// process.stderr.on('data', (data) => {
// require('fs').writeFileSync('temp-repl-output', 'monkey-12345');
// });

// setTimeout(() => {
// console.error('monkey')
// }, 5000);

const repl = new REPL({
events: this.events,
env: this.env,
inputStream: this.terminalReadableStream,
outputStream: terminalWritableStream,
logText: this.logText
}).start(() => {
this.terminal.focus();
});
}

availableServices(_services) {
Expand Down Expand Up @@ -89,7 +145,7 @@ class Monitor {
width: "100%",
height: "55%",
left: "0%",
top: "42%",
top: "40%",
border: {
type: "line"
},
Expand Down Expand Up @@ -127,7 +183,7 @@ class Monitor {
tags: true,
padding: 1,
width: "75%",
height: "42%",
height: "40%",
left: "0%",
top: "0",
border: {
Expand Down Expand Up @@ -287,97 +343,41 @@ class Monitor {
this.screen.append(this.wrapper);
}

layoutCmd() {
this.consoleBox = blessed.box({
label: __('Console'),
tags: true,
padding: 0,
width: '100%',
height: '6%',
left: '0%',
top: '95%',
border: {
type: 'line'
},
style: {
fg: 'black',
border: {
fg: this.color
}
}
});

this.input = blessed.textbox({
parent: this.consoleBox,
name: 'input',
input: true,
keys: false,
top: 0,
left: 1,
height: '50%',
width: '100%-2',
inputOnFocus: true,
style: {
fg: 'green',
bg: 'black',
focus: {
bg: 'black',
fg: 'green'
layoutTerminal() {
this.terminal = blessed.terminal({
parent: this.screen,
cursor: 'block',
cursorBlink: true,
padding: 0,
width: '100%',
height: 3,
left: 0,
top: '100%-3',
border: 'line',
style: {
fg: 'default',
bg: 'default',
focus: {
border: {
fg: 'green'
}
}
},
scrollable: false,
handler: (data) => {
this.terminalReadableStream.push(data);
}
}
});

let self = this;

this.input.key(["C-c"], function () {
self.events.emit('exit');
process.exit(0);
});

this.input.key(["C-w"], function () {
self.input.clearValue();
self.input.focus();
});
});

this.input.key(["up"], function () {
let cmd = self.history.getPreviousCommand();
self.input.setValue(cmd);
self.input.focus();
});

this.input.key(["down"], function () {
let cmd = self.history.getNextCommand();
self.input.setValue(cmd);
self.input.focus();
});

this.input.on('submit', this.submitCmd.bind(this));

this.screen.append(this.consoleBox);
}
this.terminal.key('C-c', () => {
this.terminal.kill();
return screen.destroy();
});

submitCmd(cmd) {
if (cmd !== '') {
this.history.addCommand(cmd);
this.executeCmd(cmd);
}
this.input.clearValue();
this.input.focus();
this.terminal.on('click', () => {
this.terminal.focus();
});
}

executeCmd(cmd, cb) {
this.logText.log('console> '.bold.green + cmd);
this.events.request('console:executeCmd', cmd, (err, result) => {
let message = err || result;
if (message) {
this.logText.log(message);
}
if (cb) {
cb(message);
}
});
}

}

module.exports = Monitor;
15 changes: 13 additions & 2 deletions src/cmd/dashboard/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ class REPL {
constructor(options) {
this.events = options.events;
this.env = options.env;
this.inputStream = options.inputStream || process.stdin;
this.outputStream = options.outputStream || process.stdout;
this.logText = options.logText;
}

enhancedEval(cmd, context, filename, callback) {
this.events.request('console:executeCmd', cmd.trim(), function (err, message) {
callback(err, message || ''); // This way, we don't print undefined
callback(err, message === undefined ? '' : message); // This way, we don't print undefined
});
}

enhancedWriter(output) {
if ((typeof output) === "string") {
if (this.logText) this.logText.log(output);
return output;
} else {
const inspectedOutput = util.inspect(output, {colors: true});
if (this.logText) this.logText.log(inspectedOutput);
return inspectedOutput;
}
return util.inspect(output, {colors: true});
}
Expand All @@ -26,7 +34,10 @@ class REPL {
prompt: "Embark (" + this.env + ") > ",
useGlobal: true,
eval: this.enhancedEval.bind(this),
writer: this.enhancedWriter.bind(this)
writer: this.enhancedWriter.bind(this),
input: this.inputStream,
output: this.outputStream,
terminal: true
});

this.events.request('console:history', (err, history) => {
Expand Down
17 changes: 11 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,9 @@
"@types/node" "*"

"@types/node@*":
version "10.12.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.9.tgz#a07bfa74331471e1dc22a47eb72026843f7b95c8"
integrity sha512-eajkMXG812/w3w4a1OcBlaTwsFPO5F7fJ/amy+tieQxEMWBlbV1JGSjkFM+zkHNf81Cad+dfIRA+IBkvmvdAeA==
version "10.12.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.10.tgz#4fa76e6598b7de3f0cb6ec3abacc4f59e5b3a2ce"
integrity sha512-8xZEYckCbUVgK8Eg7lf5Iy4COKJ5uXlnIOnePN0WUwSQggy9tolM+tDJf7wMOnT/JT/W9xDYIaYggt3mRV2O5w==

"@types/[email protected]":
version "10.11.7"
Expand Down Expand Up @@ -14036,6 +14036,11 @@ term-size@^1.2.0:
dependencies:
execa "^0.7.0"

[email protected]:
version "0.0.7"
resolved "https://registry.yarnpkg.com/term.js/-/term.js-0.0.7.tgz#526f24cfc0f2ef6f80f517c9e27dae947bc87315"
integrity sha1-Um8kz8Dy72+A9RfJ4n2ulHvIcxU=

test-exclude@^4.2.1:
version "4.2.3"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.3.tgz#a9a5e64474e4398339245a0a769ad7c2f4a97c20"
Expand Down Expand Up @@ -15742,9 +15747,9 @@ yaeti@^0.0.6:
integrity sha1-8m9ITXJoTPQr7ft2lwqhYI+/lXc=

yallist@^3.0.0, yallist@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9"
integrity sha1-hFK0u36Dx8GI2AQcGoN8dz1ti7k=
version "3.0.3"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9"
integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==

yargs-parser@^11.1.1:
version "11.1.1"
Expand Down

0 comments on commit 382fbc4

Please sign in to comment.