embark/lib/dashboard/console.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

let utils = require('../utils/utils.js');
let RunCode = require('../core/runCode.js');
2016-09-23 04:31:09 +00:00
2017-03-30 11:12:39 +00:00
class Console {
constructor(options) {
2017-12-17 23:34:41 +00:00
this.events = options.events;
2017-03-30 11:12:39 +00:00
this.plugins = options.plugins;
this.version = options.version;
2017-07-06 23:50:36 +00:00
this.contractsConfig = options.contractsConfig;
2016-09-25 01:23:57 +00:00
}
2017-03-30 11:12:39 +00:00
runCode(code) {
2018-02-08 00:49:54 +00:00
RunCode.doEval(code);
}
2017-03-30 11:12:39 +00:00
processEmbarkCmd (cmd) {
2018-05-08 21:49:46 +00:00
if (cmd === 'help' || cmd === __('help')) {
2017-03-30 11:12:39 +00:00
let helpText = [
2018-05-08 21:49:46 +00:00
__('Welcome to Embark') + ' ' + this.version,
2017-03-30 11:12:39 +00:00
'',
2018-05-08 21:49:46 +00:00
__('possible commands are:'),
'versions - ' + __('display versions in use for libraries and tools like web3 and solc'),
2017-03-30 11:12:39 +00:00
// TODO: only if the blockchain is actually active!
// will need to pass te current embark state here
2018-05-18 19:56:36 +00:00
'ipfs - ' + __('instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)'),
2018-05-08 21:49:46 +00:00
'web3 - ' + __('instantiated web3.js object configured to the current environment'),
'quit - ' + __('to immediatly exit (alias: exit)'),
2017-03-30 11:12:39 +00:00
'',
2018-05-08 21:49:46 +00:00
__('The web3 object and the interfaces for the deployed contracts and their methods are also available')
2017-03-30 11:12:39 +00:00
];
return helpText.join('\n');
2018-05-08 21:49:46 +00:00
} else if (['quit', 'exit', 'sair', 'sortir', __('quit')].indexOf(cmd) >= 0) {
2017-03-30 11:12:39 +00:00
utils.exit();
}
return false;
}
2016-09-23 04:31:09 +00:00
2017-03-30 11:12:39 +00:00
executeCmd(cmd, callback) {
2017-12-29 23:21:36 +00:00
var pluginCmds = this.plugins.getPluginsProperty('console', 'console');
for (let pluginCmd of pluginCmds) {
let pluginOutput = pluginCmd.call(this, cmd, {});
if (pluginOutput !== false && pluginOutput !== 'false' && pluginOutput !== undefined) return callback(pluginOutput);
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
let output = this.processEmbarkCmd(cmd);
if (output) {
return callback(output);
}
2017-03-30 11:12:39 +00:00
try {
let result = RunCode.doEval(cmd);
return callback(result);
}
catch (e) {
if (e.message.indexOf('not defined') > 0) {
2018-05-08 21:49:46 +00:00
return callback(("error: " + e.message).red + ("\n" + __("Type") + " " + "help".bold + " " + __("to see the list of available commands")).cyan);
2017-03-30 11:12:39 +00:00
} else {
return callback(e.message);
}
}
2016-09-23 04:31:09 +00:00
}
2017-03-30 11:12:39 +00:00
}
2016-09-23 04:31:09 +00:00
module.exports = Console;