mirror of
https://github.com/embarklabs/embark.git
synced 2025-01-27 14:05:27 +00:00
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:
parent
e738efe15a
commit
3229e15841
@ -1,6 +1,5 @@
|
||||
const repl = require("repl");
|
||||
const util = require("util");
|
||||
let fs = require('../../lib/core/fs');
|
||||
|
||||
class REPL {
|
||||
constructor(options) {
|
||||
@ -21,17 +20,15 @@ class REPL {
|
||||
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});
|
||||
const inspectedOutput = util.inspect(output, {colors: true});
|
||||
if (this.logText) this.logText.log(inspectedOutput);
|
||||
return inspectedOutput;
|
||||
}
|
||||
|
||||
start(done) {
|
||||
this.replServer = repl.start({
|
||||
prompt: "Embark (" + this.env + ") > ",
|
||||
prompt: "Embark (".cyan + this.env.green + ") > ".cyan,
|
||||
useGlobal: true,
|
||||
eval: this.enhancedEval.bind(this),
|
||||
writer: this.enhancedWriter.bind(this),
|
||||
|
@ -160,8 +160,12 @@ Plugin.prototype.addContractFile = function(file) {
|
||||
this.addPluginType('contractFiles');
|
||||
};
|
||||
|
||||
Plugin.prototype.registerConsoleCommand = function(cb) {
|
||||
this.console.push(cb);
|
||||
Plugin.prototype.registerConsoleCommand = function(options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
this.console.push({options, execute: cb});
|
||||
this.addPluginType('console');
|
||||
};
|
||||
|
||||
|
@ -72,7 +72,7 @@ class Console {
|
||||
});
|
||||
}
|
||||
|
||||
private processEmbarkCmd(cmd: string) {
|
||||
private processEmbarkCmd(cmd: string, helpDescriptions: any[]) {
|
||||
if (cmd === "help" || cmd === __("help") || cmd === "01189998819991197253") {
|
||||
const helpText = [
|
||||
__("Welcome to Embark") + " " + this.version,
|
||||
@ -96,10 +96,14 @@ class Console {
|
||||
" 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, "),
|
||||
"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");
|
||||
} else if (["quit", "exit", "sair", "sortir", __("quit")].indexOf(cmd) >= 0) {
|
||||
utils.exit();
|
||||
@ -112,9 +116,24 @@ class Console {
|
||||
this.history.push(cmd);
|
||||
this.saveHistory();
|
||||
}
|
||||
const pluginCmds = this.plugins.getPluginsProperty("console", "console");
|
||||
for (const pluginCmd of pluginCmds) {
|
||||
const pluginResult = pluginCmd.call(this, cmd, {});
|
||||
const plugins = this.plugins.getPluginsProperty("console", "console");
|
||||
const helpDescriptions = [];
|
||||
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 (pluginResult !== false && pluginResult !== "false" && pluginResult !== undefined) {
|
||||
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) {
|
||||
return callback(null, output);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user