add console plugin support

This commit is contained in:
Iuri Matias 2017-01-16 07:00:41 -05:00
parent 0e0b0c99d3
commit 352eadab98
5 changed files with 56 additions and 2 deletions

View File

@ -7,5 +7,5 @@
},
"buildDir": "dist/",
"config": "config/",
"plugins": []
"plugins": {}
}

View File

@ -108,3 +108,21 @@ expected return: ``string``
});
}
**embark.registerConsoleCommand(callback(options))**
This call is used to extend the console with custom commands.
expected return: ``string`` (output to print in console) or ``boolean`` (skip command if false)
.. code:: javascript
module.exports = function(embark) {
embark.registerConsoleCommand(function(cmd, options) {
if (cmd === "hello") {
return "hello there!";
}
// continue to embark or next plugin;
return false;
});
}

View File

@ -1,6 +1,7 @@
var Web3 = require('web3');
var Console = function(options) {
this.plugins = options.plugins;
};
Console.prototype.runCode = function(code) {
@ -8,6 +9,14 @@ Console.prototype.runCode = function(code) {
};
Console.prototype.executeCmd = function(cmd, callback) {
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);
}
if (cmd === 'help') {
var helpText = [
'Welcome to Embark 2',

View File

@ -79,7 +79,7 @@ var Embark = {
callback();
},
function startConsole(callback) {
Embark.console = new Console();
Embark.console = new Console({plugins: self.plugins});
callback();
},
function startMonitor(callback) {

View File

@ -9,6 +9,7 @@ var Plugin = function(options) {
this.clientWeb3Providers = [];
this.contractsGenerators = [];
this.pipeline = [];
this.console = [];
this.pluginTypes = [];
this.logger = options.logger;
};
@ -26,6 +27,21 @@ Plugin.prototype.interceptLogs = function(context) {
//self.logger.error.apply(self.logger, arguments);
self.logger.error(self.name + " > " + txt);
};
context.console.log = function(txt) {
self.logger.info(self.name + " > " + txt);
};
context.console.warn = function(txt) {
self.logger.warn(self.name + " > " + txt);
};
context.console.info = function(txt) {
self.logger.info(self.name + " > " + txt);
};
context.console.debug = function(txt) {
self.logger.debug(self.name + " > " + txt);
};
context.console.trace = function(txt) {
self.logger.trace(self.name + " > " + txt);
};
};
// TODO: add deploy provider
@ -45,6 +61,11 @@ Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
this.pluginTypes.push('pipeline');
};
Plugin.prototype.registerConsoleCommand = function(cb) {
this.console.push(cb);
this.pluginTypes.push('console');
};
Plugin.prototype.has = function(pluginType) {
return this.pluginTypes.indexOf(pluginType) >= 0;
};
@ -61,6 +82,12 @@ Plugin.prototype.generateContracts = function(args) {
}).join("\n");
};
Plugin.prototype.runCommands = function(cmd, options) {
return this.console.map(function(cb) {
return cb.call(this, cmd, options);
}).join("\n");
};
Plugin.prototype.runPipeline = function(args) {
// TODO: should iterate the pipeliens
var pipeline = this.pipeline[0];