feat(console): add new api to register console commands

Enables us to add help commands without putting the logic in console
This commit is contained in:
Jonathan Rainville 2018-12-13 11:07:36 -05:00
parent e738efe15a
commit 3229e15841
3 changed files with 37 additions and 17 deletions

View File

@ -1,6 +1,5 @@
const repl = require("repl"); const repl = require("repl");
const util = require("util"); const util = require("util");
let fs = require('../../lib/core/fs');
class REPL { class REPL {
constructor(options) { constructor(options) {
@ -21,17 +20,15 @@ class REPL {
if ((typeof output) === "string") { if ((typeof output) === "string") {
if (this.logText) this.logText.log(output); if (this.logText) this.logText.log(output);
return 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}); const inspectedOutput = util.inspect(output, {colors: true});
if (this.logText) this.logText.log(inspectedOutput);
return inspectedOutput;
} }
start(done) { start(done) {
this.replServer = repl.start({ this.replServer = repl.start({
prompt: "Embark (" + this.env + ") > ", prompt: "Embark (".cyan + this.env.green + ") > ".cyan,
useGlobal: true, useGlobal: true,
eval: this.enhancedEval.bind(this), eval: this.enhancedEval.bind(this),
writer: this.enhancedWriter.bind(this), writer: this.enhancedWriter.bind(this),

View File

@ -160,8 +160,12 @@ Plugin.prototype.addContractFile = function(file) {
this.addPluginType('contractFiles'); this.addPluginType('contractFiles');
}; };
Plugin.prototype.registerConsoleCommand = function(cb) { Plugin.prototype.registerConsoleCommand = function(options, cb) {
this.console.push(cb); if (typeof options === 'function') {
cb = options;
options = {};
}
this.console.push({options, execute: cb});
this.addPluginType('console'); this.addPluginType('console');
}; };

View File

@ -72,7 +72,7 @@ class Console {
}); });
} }
private processEmbarkCmd(cmd: string) { private processEmbarkCmd(cmd: string, helpDescriptions: any[]) {
if (cmd === "help" || cmd === __("help") || cmd === "01189998819991197253") { if (cmd === "help" || cmd === __("help") || cmd === "01189998819991197253") {
const helpText = [ const helpText = [
__("Welcome to Embark") + " " + this.version, __("Welcome to Embark") + " " + this.version,
@ -96,10 +96,14 @@ class Console {
" var all/v a/va - " + __("During a debug, display all variables"), " var all/v a/va - " + __("During a debug, display all variables"),
"log <process> on/off - " + __("Activate or deactivate the logs of a sub-process. Options: blockchain, "), "log <process> on/off - " + __("Activate or deactivate the logs of a sub-process. Options: blockchain, "),
"plugin install <package> - " + __("Installs a plugin in the Dapp. eg: plugin install embark-solc"), "plugin install <package> - " + __("Installs a plugin in the Dapp. eg: plugin install embark-solc"),
"quit - " + __("to immediatly exit (alias: exit)"),
"",
__("The web3 object and the interfaces for the deployed contracts and their methods are also available"),
]; ];
helpDescriptions.forEach((helpDescription) => {
helpText.push(`${(helpDescription.use || helpDescription.matches.join("/")).cyan} - ${helpDescription.description}`);
});
// Add end commands
helpText.push("quit".cyan + " - " + __("to immediatly exit (alias: exit)"),
"",
__("The web3 object and the interfaces for the deployed contracts and their methods are also available"));
return helpText.join("\n"); return helpText.join("\n");
} else if (["quit", "exit", "sair", "sortir", __("quit")].indexOf(cmd) >= 0) { } else if (["quit", "exit", "sair", "sortir", __("quit")].indexOf(cmd) >= 0) {
utils.exit(); utils.exit();
@ -112,9 +116,24 @@ class Console {
this.history.push(cmd); this.history.push(cmd);
this.saveHistory(); this.saveHistory();
} }
const pluginCmds = this.plugins.getPluginsProperty("console", "console"); const plugins = this.plugins.getPluginsProperty("console", "console");
for (const pluginCmd of pluginCmds) { const helpDescriptions = [];
const pluginResult = pluginCmd.call(this, cmd, {}); for (const plugin of plugins) {
// New API
if (plugin.options.description) {
helpDescriptions.push(plugin.options);
}
if (plugin.options.matches) {
const isFunction = typeof plugin.options.matches === "function";
if ((isFunction && plugin.options.matches.call(this, cmd))
|| (!isFunction && plugin.options.matches.includes(cmd))) {
return plugin.execute.call(this, cmd, callback);
}
continue;
}
const pluginResult = plugin.execute.call(this, cmd, {});
if (typeof pluginResult !== "object") { if (typeof pluginResult !== "object") {
if (pluginResult !== false && pluginResult !== "false" && pluginResult !== undefined) { if (pluginResult !== false && pluginResult !== "false" && pluginResult !== undefined) {
this.logger.warn("[DEPRECATED] In future versions of embark, we expect the console command to return an object " + this.logger.warn("[DEPRECATED] In future versions of embark, we expect the console command to return an object " +
@ -127,7 +146,7 @@ class Console {
} }
} }
const output = this.processEmbarkCmd(cmd); const output = this.processEmbarkCmd(cmd, helpDescriptions);
if (output) { if (output) {
return callback(null, output); return callback(null, output);
} }