diff --git a/boilerplate/embark.json b/boilerplate/embark.json index dcee5f59..5f43498a 100644 --- a/boilerplate/embark.json +++ b/boilerplate/embark.json @@ -7,5 +7,5 @@ }, "buildDir": "dist/", "config": "config/", - "plugins": [] + "plugins": {} } diff --git a/docs/plugins.rst b/docs/plugins.rst index 76dc1479..d50cd7cc 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -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; + }); + } + diff --git a/lib/console.js b/lib/console.js index 69e56ed2..6e57e0ff 100644 --- a/lib/console.js +++ b/lib/console.js @@ -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', diff --git a/lib/index.js b/lib/index.js index 6868f235..238cbe38 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/lib/plugin.js b/lib/plugin.js index 8e4e6931..21ba340f 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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];