embark-area-51/lib/dashboard/console.js

64 lines
1.7 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
let Console = function(options) {
2017-01-16 12:00:41 +00:00
this.plugins = options.plugins;
this.version = options.version;
2016-09-23 04:31:09 +00:00
};
Console.prototype.runCode = function(code) {
2017-02-18 21:53:49 +00:00
RunCode.doEval(code); // jshint ignore:line
2016-09-23 04:31:09 +00:00
};
Console.prototype.processEmbarkCmd = function(cmd) {
2016-09-23 04:31:09 +00:00
if (cmd === 'help') {
let helpText = [
'Welcome to Embark ' + this.version,
2016-09-23 04:31:09 +00:00
'',
'possible commands are:',
2017-02-16 01:35:42 +00:00
// TODO: only if the blockchain is actually active!
// will need to pass te current embark state here
'web3 - instantiated web3.js object configured to the current environment',
2016-09-23 04:31:09 +00:00
'quit - to immediatly exit',
'',
2017-02-22 13:10:33 +00:00
'The web3 object and the interfaces for the deployed contracts and their methods are also available'
2016-09-23 04:31:09 +00:00
];
return helpText.join('\n');
2016-09-23 04:31:09 +00:00
} else if (cmd === 'quit') {
utils.exit();
2016-09-25 01:23:57 +00:00
}
return false;
};
Console.prototype.executeCmd = function(cmd, callback) {
let plugin, pluginOutput;
let plugins = [];
this.plugins.emit('get', 'console', (list) => {
plugins = list;
});
for (let i = 0; i < plugins.length; i++) {
plugin = plugins[i];
pluginOutput = plugin.runCommands(cmd, {});
if (pluginOutput !== false && pluginOutput !== 'false') return callback(pluginOutput);
}
let output = this.processEmbarkCmd(cmd);
if (output) {
return callback(output);
}
2016-09-23 04:31:09 +00:00
try {
let result = RunCode.doEval(cmd);
2016-09-23 04:31:09 +00:00
return callback(result);
}
catch(e) {
if (e.message.indexOf('not defined') > 0) {
return callback(("error: " + e.message).red + ("\nType " + "help".bold + " to see the list of available commands").cyan);
} else {
return callback(e.message);
}
2016-09-23 04:31:09 +00:00
}
};
module.exports = Console;