From 6f249df4bfe76923a43522bbc1723c19e5091f57 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 11 May 2018 15:49:06 -0400 Subject: [PATCH] Fixed plugin loading logic --- .../index.js => cmds/scaffolding.js} | 33 ++++++++++--------- lib/core/plugin.js | 6 ++-- lib/index.js | 11 +++---- lib/modules/scaffolding-react/index.js | 4 +++ 4 files changed, 30 insertions(+), 24 deletions(-) rename lib/{modules/scaffolding/index.js => cmds/scaffolding.js} (55%) diff --git a/lib/modules/scaffolding/index.js b/lib/cmds/scaffolding.js similarity index 55% rename from lib/modules/scaffolding/index.js rename to lib/cmds/scaffolding.js index 67797847..73416d93 100644 --- a/lib/modules/scaffolding/index.js +++ b/lib/cmds/scaffolding.js @@ -1,5 +1,3 @@ -const fs = require('fs'); - const commandName = "generate-ui"; const errorMessage = (message) => new Error(commandName + ": " + message); @@ -16,30 +14,33 @@ class Scaffolding { return this.embark.config.contractsConfig.contracts[contractName] !== undefined; } - generate(contractName){ - let frameworkPlugin; - + generate(contractName, contractConfiguration){ if(this.framework == 'react'){ this.embark.plugins.loadInternalPlugin('scaffolding-react', this.options); - frameworkPlugin = this.embark.plugins.plugins.filter(x => x.name == "scaffolding-react")[0].pluginModule; - } else { - let plugins = this.embark.plugins.getPluginsFor(this.framework); - if(plugins.length !== 1){ - throw errorMessage("Could not find plugin for framework '" + this.framework + "'"); - } - frameworkPlugin = plugins[0].pluginModule; } + + let dappGenerators = this.embark.plugins.getPluginsFor('dappGenerator'); + let build = null; + dappGenerators.forEach((plugin) => { + plugin.dappGenerators.forEach((d) => { + if(d.framework == this.framework){ + build = d.cb; + } + }); + }); - + if(build === null){ + throw errorMessage("Could not find plugin for framework '" + this.framework + "'"); + } + if(!this.isContract(contractName)){ return errorMessage("contract '" + contractName + "' does not exist"); } - const contract = this.embark.config.contractsConfig.contracts[contractName]; + const contract = contractConfiguration.contracts[contractName]; try { - let uiFramework = new frameworkPlugin(this.embark, this.options); - let result = uiFramework.build(contract); + let result = build(contract); this.embark.logger.info(result); } catch(err){ throw errorMessage(err); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index f7d6c8a7..866585a5 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -20,6 +20,7 @@ var Plugin = function(options) { this.contractsFiles = []; this.compilers = []; this.serviceChecks = []; + this.dappGenerators = []; this.pluginTypes = []; this.uploadCmds = []; this.imports = []; @@ -123,8 +124,9 @@ Plugin.prototype.registerPipeline = function(matcthingFiles, cb) { this.addPluginType('pipeline'); }; -Plugin.prototype.registerCustomType = function(type){ - this.pluginTypes.push(type); +Plugin.prototype.registerDappGenerator = function(framework, cb){ + this.dappGenerators.push({framework: framework, cb: cb}); + this.pluginTypes.push('dappGenerator'); }; Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) { diff --git a/lib/index.js b/lib/index.js index adcb88d0..daf57ead 100644 --- a/lib/index.js +++ b/lib/index.js @@ -359,6 +359,8 @@ class Embark { } scaffold(options) { + const Scaffolding = require('./cmds/scaffolding.js'); + // initialise embark engine let engine = new Engine({ env: options.env, @@ -368,16 +370,13 @@ class Embark { }); engine.init(); - // load plugins - this.plugins.loadInternalPlugin('scaffolding', options); - let scaffoldPlugin = this.plugins.plugins.filter(x => x.name == "scaffolding")[0].pluginModule; let self = this; - + async.waterfall([ function generateUI(callback){ engine.events.on('contractsDeployed', function () { - let scaffold = new scaffoldPlugin(engine, options); - let result = scaffold.generate(options.contract); + let scaffold = new Scaffolding(self, options); + let result = scaffold.generate(options.contract, engine.config.contractsConfig); engine.logger.info(result); callback(); }); diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 77ae40dd..9885b35c 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -49,6 +49,8 @@ class ScaffoldingReact { constructor(embark, options){ this.embark = embark; this.options = options; + + this.embark.registerDappGenerator('react', this.build.bind(this)); } _generateFile(contract, templateFilename, extension, data){ @@ -95,6 +97,8 @@ class ScaffoldingReact { embarkJson.app[filename + ".html"] = "app/" + filename + '.html'; fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4)); + + return filename + ".html generated"; } }