2017-03-29 15:37:30 +00:00
|
|
|
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) {
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.version = options.version;
|
2016-09-25 01:23:57 +00:00
|
|
|
}
|
2017-03-02 12:44:24 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
runCode(code) {
|
2017-07-06 22:48:20 +00:00
|
|
|
try {
|
|
|
|
RunCode.doEval(code); // jshint ignore:line
|
|
|
|
} catch(e) {
|
|
|
|
console.log(code);
|
|
|
|
console.debug(e);
|
|
|
|
}
|
2017-03-02 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
processEmbarkCmd (cmd) {
|
|
|
|
if (cmd === 'help') {
|
|
|
|
let helpText = [
|
|
|
|
'Welcome to Embark ' + this.version,
|
|
|
|
'',
|
|
|
|
'possible commands are:',
|
|
|
|
// 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',
|
|
|
|
'quit - to immediatly exit',
|
|
|
|
'',
|
|
|
|
'The web3 object and the interfaces for the deployed contracts and their methods are also available'
|
|
|
|
];
|
|
|
|
return helpText.join('\n');
|
|
|
|
} else if (cmd === 'quit') {
|
|
|
|
utils.exit();
|
|
|
|
}
|
|
|
|
return false;
|
2017-03-02 12:44:24 +00:00
|
|
|
}
|
2016-09-23 04:31:09 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
executeCmd(cmd, callback) {
|
2017-03-31 11:34:43 +00:00
|
|
|
var plugin, pluginOutput;
|
|
|
|
var plugins = this.plugins.getPluginsFor('console');
|
|
|
|
for (var i = 0; i < plugins.length; i++) {
|
2017-03-30 11:12:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let result = RunCode.doEval(cmd);
|
|
|
|
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);
|
|
|
|
}
|
2017-02-16 01:24:42 +00:00
|
|
|
}
|
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;
|