From adf7340ab8f27f553710fc79db9cbd85fd3dc690 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 | 24 ++++++++++ lib/core/plugin.js | 4 ++ lib/index.js | 36 +++++++++++++++ lib/modules/scaffolding-react/index.js | 14 ++++++ lib/modules/scaffolding/index.js | 61 ++++++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 lib/modules/scaffolding-react/index.js create mode 100644 lib/modules/scaffolding/index.js diff --git a/cmd/cmd.js b/cmd/cmd.js index 97a3ff66..fd7f34ea 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -19,6 +19,7 @@ class Cmd { this.test(); this.reset(); this.graph(); + this.scaffold(); this.upload(); this.versionCmd(); this.helpCmd(); @@ -261,6 +262,29 @@ class Cmd { }); } + scaffold() { + program + .command('scaffold [contract] [environment]') + .option('--framework', 'UI framework to use. (default: react)') + .action(function(contract, env, options){ + let environment = env || 'development'; + + if(contract === undefined){ + console.log("contract name is required"); + process.exit(0); + } + + embark.initConfig(environment, { + embarkConfig: 'embark.json', interceptLogs: false + }); + + options.contract = contract; + options.framework = options.framework || 'react'; + options.env = environment; + embark.scaffold(options); + }); + } + reset() { program .command('reset') diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 80e14ec0..160b5928 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -118,6 +118,10 @@ Plugin.prototype.registerPipeline = function(matcthingFiles, cb) { this.addPluginType('pipeline'); }; +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/index.js b/lib/index.js index 61d1fd02..f341224a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -358,6 +358,42 @@ class Embark { resetCmd(); } + scaffold(options) { + // initialise embark engine + let engine = new Engine({ + env: options.env, + version: this.version, + embarkConfig: options.embarkConfig || 'embark.json', + logfile: options.logfile + }); + 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('outputDone', function () { + let scaffold = new scaffoldPlugin(engine, options); + let result = scaffold.generate(options.contract); + engine.logger.info(result); + callback(); + }); + self.build(options, engine, true); + } + ], function (err, _result) { + if (err) { + engine.logger.error(err.message); + engine.logger.debug(err.stack); + } else { + engine.logger.info("Finished generating ui"); + } + process.exit(); + }); + } + upload(options) { this.context = options.context || [constants.contexts.upload, constants.contexts.build]; diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js new file mode 100644 index 00000000..ec79d060 --- /dev/null +++ b/lib/modules/scaffolding-react/index.js @@ -0,0 +1,14 @@ + +class ScaffoldingReact { + constructor(embark, options){ + this.embark = embark; + this.options = options; + } + + build(contract){ + + } +} + + +module.exports = ScaffoldingReact; diff --git a/lib/modules/scaffolding/index.js b/lib/modules/scaffolding/index.js new file mode 100644 index 00000000..1bf099bf --- /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;