From 8861588726380c3d310cb9f7f23e8446d49cfa2f Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 9 May 2018 17:02:17 -0400 Subject: [PATCH] Adding base command logic for scaffolding generation --- cmd/cmd.js | 3 +- lib/core/plugin.js | 4 ++ lib/modules/scaffolding-react/index.js | 1 - lib/modules/scaffolding/index.js | 61 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 lib/modules/scaffolding/index.js diff --git a/cmd/cmd.js b/cmd/cmd.js index 9a3ef189a..8f0bab811 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -329,7 +329,6 @@ class Cmd { .command('scaffold [contract] [environment]') .option('--framework ', 'UI framework to use. (default: react)') .action(function(contract, env, options){ - let environment = env || 'development'; if(contract === undefined){ @@ -342,7 +341,7 @@ class Cmd { }); options.contract = contract; - options.framework = options.framework || 'react'; + options.framework = options.framework || 'react'; options.env = environment; embark.scaffold(options); }); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 866585a56..5e9b84256 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -129,6 +129,10 @@ Plugin.prototype.registerDappGenerator = function(framework, cb){ this.pluginTypes.push('dappGenerator'); }; +Plugin.prototype.registerCustomType = function(type){ + this.pluginTypes.push(type); +} + Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) { this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options}); this.addPluginType('pipelineFiles'); diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 5522c0193..3457cfc4d 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -45,7 +45,6 @@ class ScaffoldingReact { constructor(embark, options){ this.embark = embark; this.options = options; - this.embark.registerDappGenerator('react', this.build.bind(this)); } diff --git a/lib/modules/scaffolding/index.js b/lib/modules/scaffolding/index.js new file mode 100644 index 000000000..1bf099bf6 --- /dev/null +++ b/lib/modules/scaffolding/index.js @@ -0,0 +1,61 @@ +const fs = require('fs'); + +const commandName = "generate-ui"; + +const formatReplyMsg = (message) => commandName + ": " + message; + +class Scaffolding { + constructor(embark, options){ + this.embark = embark; + this.options = options; + this.framework = options.framework; + this.frameworkPlugin = null; + } + + createDirectories(contractName){ + const dir = './app/' + contractName; + if (!fs.existsSync(dir)){ + fs.mkdirSync(dir); + } else { + throw formatReplyMsg("directory ./app/" + contractName + " already exists"); + } + } + + isContract(contractName){ + return this.embark.config.contractsConfig.contracts[contractName] !== undefined; + } + + generate(contractName){ + let frameworkPlugin; + + 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){ + return formatReplyMsg("Could not find plugin for framework '" + this.framework + "'"); + } + frameworkPlugin = plugins[0].pluginModule; + } + + try { + if(!this.isContract(contractName)){ + return formatReplyMsg("contract '" + contractName + "' does not exist"); + } + + const contract = this.embark.config.contractsConfig.contracts[contractName]; + + this.createDirectories(contractName); + let uiFramework = new frameworkPlugin(this.embark, this.options); + uiFramework.build(contract); + } catch(err){ + return err; + } + + return formatReplyMsg("done!"); + } +} + + +module.exports = Scaffolding;