From 2c0644b5cb3485add05f2c3cb4a47ee4564c5309 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 5 Oct 2018 19:13:00 -0400 Subject: [PATCH] Changed scaffolding to service --- cmd/cmd_controller.js | 9 +-- lib/cmds/scaffolding.js | 54 ------------------ lib/constants.json | 1 + lib/core/engine.js | 8 ++- lib/modules/scaffolding-react/index.js | 55 +++++++++--------- lib/modules/scaffolding/index.js | 78 +++++++++++++------------- 6 files changed, 83 insertions(+), 122 deletions(-) delete mode 100644 lib/cmds/scaffolding.js diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 9a1e7367b..f7c46b1ef 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -435,9 +435,7 @@ class EmbarkController { this.context = options.context || [constants.contexts.scaffold]; options.onlyCompile = true; - const Scaffolding = require('../lib/cmds/scaffolding.js'); const Engine = require('../lib/core/engine.js'); - const engine = new Engine({ env: options.env, version: this.version, @@ -463,6 +461,7 @@ class EmbarkController { engine.startService("pipeline"); engine.startService("deployment", {onlyCompile: true}); engine.startService("web3"); + engine.startService("scaffolding"); engine.events.request('deploy:contracts', callback); } @@ -471,8 +470,10 @@ class EmbarkController { engine.logger.error(err.message); engine.logger.info(err.stack); } else { - let scaffold = new Scaffolding(engine, options); - scaffold.generate(options.contract, options.overwrite); + engine.events.request("scaffolding:generate", options, () => { + engine.logger.info(__("finished generating the UI").underline); + process.exit(); + }); } }); } diff --git a/lib/cmds/scaffolding.js b/lib/cmds/scaffolding.js deleted file mode 100644 index a2ca765f9..000000000 --- a/lib/cmds/scaffolding.js +++ /dev/null @@ -1,54 +0,0 @@ -class Scaffolding { - constructor(engine, options){ - this.engine = engine; - this.options = options; - this.framework = options.framework; - this.frameworkPlugin = null; - } - - isContract(contractName){ - return this.engine.config.contractsConfig.contracts[contractName] !== undefined; - } - - generate(contractName, overwrite){ - if(this.framework === 'react'){ - this.engine.plugins.loadInternalPlugin('scaffolding-react', this.options); - } - - let dappGenerators = this.engine.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 new Error("Could not find plugin for framework '" + this.framework + "'"); - } - - if(!this.isContract(contractName)){ - throw new Error("contract '" + contractName + "' does not exist"); - } - - this.engine.events.request("contracts:list", (_err, contractsList) => { - if(_err) throw new Error(_err); - - const contract = contractsList.find(x => x.className === contractName); - try { - build(contract, overwrite, () => { - this.engine.logger.info(__("finished generating the UI").underline); - process.exit(); - }); - } catch(err){ - this.engine.logger.error(err.message); - } - }); - } -} - - -module.exports = Scaffolding; diff --git a/lib/constants.json b/lib/constants.json index 894efa368..3bbe63ac3 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -9,6 +9,7 @@ "build": "build", "console": "console", "graph": "graph", + "scaffold": "scaffold", "test": "test", "reset": "reset", "any": "any" diff --git a/lib/core/engine.js b/lib/core/engine.js index d03049d30..72e10f78d 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -75,8 +75,10 @@ class Engine { "storage": this.storageService, "pluginCommand": this.pluginCommandService "graph": this.graphService, + "pluginCommand": this.pluginCommandService, + "testRunner": this.testRunnerService, "codeCoverage": this.codeCoverageService, - "testRunner": this.testRunnerService + "scaffolding": this.scaffoldingService }; let service = services[serviceName]; @@ -103,6 +105,10 @@ class Engine { this.registerModule('graph'); } + scaffoldingService(_options) { + this.registerModule('scaffolding', {plugins: this.plugins}); + } + pipelineService(_options) { const self = this; this.registerModule('pipeline', { diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 73db137d4..167923d27 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -74,32 +74,37 @@ class ScaffoldingReact { process.exit(1); } - const filename = contract.className.toLowerCase(); - - this._generateFile(contract, 'index.html.tpl', 'html', - { - 'title': contract.className, - filename - }, overwrite); - - this._generateFile(contract, 'dapp.js.tpl', 'js', - { - 'title': contract.className, - 'contractName': contract.className, - 'functions': contract.abiDefinition.filter(x => x.type === 'function') - }, overwrite); - - // Update config - const contents = fs.readFileSync("./embark.json"); - let embarkJson = JSON.parse(contents); - embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js'; - embarkJson.app[filename + ".html"] = "app/" + filename + '.html'; - - fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4)); - - this.embark.logger.info(filename + ".html generated"); - this.embark.logger.info(filename + ".js generated"); + try { + const filename = contract.className.toLowerCase(); + this._generateFile(contract, 'index.html.tpl', 'html', + { + 'title': contract.className, + filename + }, overwrite); + + this._generateFile(contract, 'dapp.js.tpl', 'js', + { + 'title': contract.className, + 'contractName': contract.className, + 'functions': contract.abiDefinition.filter(x => x.type === 'function') + }, overwrite); + + // Update config + const contents = fs.readFileSync("./embark.json"); + let embarkJson = JSON.parse(contents); + embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js'; + embarkJson.app[filename + ".html"] = "app/" + filename + '.html'; + + fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4)); + + this.embark.logger.info(filename + ".html generated"); + this.embark.logger.info(filename + ".js generated"); + + } catch(error){ + this.embark.logger.error(error.message); + process.exit(1); + } cb(); }); } diff --git a/lib/modules/scaffolding/index.js b/lib/modules/scaffolding/index.js index 3f40fbac6..197063cd4 100644 --- a/lib/modules/scaffolding/index.js +++ b/lib/modules/scaffolding/index.js @@ -1,50 +1,52 @@ -const fs = require('fs'); - -const commandName = "generate-ui"; - -//const errorMessage = (message) => new Error(commandName + ": " + message); -const formatReplyMsg = (message) => commandName + ": " + message; - class Scaffolding { - constructor(embark, options){ - this.embark = embark; - this.options = options; - this.framework = options.framework; - this.frameworkPlugin = null; + constructor(engine, _options){ + this.engine = engine; + this.options = _options; + this.plugins = _options.plugins; + + engine.events.setCommandHandler("scaffolding:generate", (options, cb) => { + this.framework = options.framework; + this.generate(options.contract, options.overwrite, cb); + }); } isContract(contractName){ - return this.embark.config.contractsConfig.contracts[contractName] !== undefined; + return this.engine.config.contractsConfig.contracts[contractName] !== undefined; } - generate(contractName){ - let frameworkPlugin; + generate(contractName, overwrite, cb){ + if(this.framework === 'react'){ + this.plugins.loadInternalPlugin('scaffolding-react', this.options); + } - 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; + let dappGenerators = this.plugins.getPluginsFor('dappGenerator'); + + let build = null; + dappGenerators.forEach((plugin) => { + plugin.dappGenerators.forEach((d) => { + if(d.framework === this.framework){ + build = d.cb; + } + }); + }); + + if(build === null){ + this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'"); + cb(); + } else if(!this.isContract(contractName)){ + this.engine.logger.error("contract '" + contractName + "' does not exist"); + cb(); } else { - let plugins = this.embark.plugins.getPluginsFor(this.framework); - if(plugins.length !== 1){ - //throw errorMessage("Could not find plugin for framework '" + this.framework + "'"); - return formatReplyMsg("Could not find plugin for framework '" + this.framework + "'"); - } - frameworkPlugin = plugins[0].pluginModule; - } - - - if(!this.isContract(contractName)){ - return errorMessage("contract '" + contractName + "' does not exist"); - } + this.engine.events.request("contracts:list", (_err, contractsList) => { + if(_err) throw new Error(_err); - const contract = this.embark.config.contractsConfig.contracts[contractName]; - - try { - let uiFramework = new frameworkPlugin(this.embark, this.options); - let result = uiFramework.build(contract); - this.embark.logger.info(result); - } catch(err){ - throw errorMessage(err); + const contract = contractsList.find(x => x.className === contractName); + try { + build(contract, overwrite, cb); + } catch(err){ + this.engine.logger.error(err.message); + } + }); } } }