2016-09-23 04:31:09 +00:00
|
|
|
var Web3 = require('web3');
|
|
|
|
|
|
|
|
var Console = function(options) {
|
2017-01-16 12:00:41 +00:00
|
|
|
this.plugins = options.plugins;
|
2017-02-16 01:56:18 +00:00
|
|
|
this.version = options.version;
|
2016-09-23 04:31:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Console.prototype.runCode = function(code) {
|
2016-09-25 01:23:57 +00:00
|
|
|
eval(code); // jshint ignore:line
|
2016-09-23 04:31:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Console.prototype.executeCmd = function(cmd, callback) {
|
2017-01-16 12:00:41 +00:00
|
|
|
var plugin, pluginOutput;
|
|
|
|
var plugins = this.plugins.getPluginsFor('console');
|
|
|
|
for (var i = 0; i < plugins.length; i++) {
|
|
|
|
plugin = plugins[i];
|
|
|
|
pluginOutput = plugin.runCommands(cmd, {});
|
|
|
|
if (pluginOutput !== false && pluginOutput !== 'false') return callback(pluginOutput);
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:31:09 +00:00
|
|
|
if (cmd === 'help') {
|
|
|
|
var helpText = [
|
2017-02-16 01:56:18 +00:00
|
|
|
'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',
|
|
|
|
'',
|
|
|
|
'The web3 object and the interfaces for the deployed contrats and their methods are also available'
|
|
|
|
];
|
|
|
|
return callback(helpText.join('\n'));
|
|
|
|
} else if (cmd === 'quit') {
|
|
|
|
exit();
|
2016-09-25 01:23:57 +00:00
|
|
|
}
|
2016-09-23 04:31:09 +00:00
|
|
|
|
|
|
|
try {
|
2016-09-25 01:23:57 +00:00
|
|
|
var result = eval(cmd); // jshint ignore:line
|
2016-09-23 04:31:09 +00:00
|
|
|
return callback(result);
|
|
|
|
}
|
|
|
|
catch(e) {
|
2017-02-16 01:24:42 +00:00
|
|
|
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;
|
|
|
|
|