From 3aac1e4e2fd9df4523a26a6851c1039d8ec7f8e0 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 10 May 2018 11:25:31 -0400 Subject: [PATCH] Addid basic templating to start building UI from here --- lib/index.js | 4 +- lib/modules/scaffolding-react/index.js | 26 ++++++++++++- .../scaffolding-react/templates/index.tpl | 12 ++++++ lib/modules/scaffolding/index.js | 38 +++++++------------ package.json | 3 +- 5 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 lib/modules/scaffolding-react/templates/index.tpl diff --git a/lib/index.js b/lib/index.js index 9ed1a360..cfa958a6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -269,13 +269,13 @@ class Embark { async.waterfall([ function generateUI(callback){ - engine.events.on('outputDone', function () { + engine.events.on('contractsDeployed', function () { let scaffold = new scaffoldPlugin(engine, options); let result = scaffold.generate(options.contract); engine.logger.info(result); callback(); }); - self.build(options, engine, true); + self.build(options, engine, false); } ], function (err, _result) { if (err) { diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index ec79d060..6c7d223b 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -1,14 +1,36 @@ +const Handlebars = require('handlebars'); + +const fs = require('../../core/fs'); +const utils = require('../../utils/utils'); class ScaffoldingReact { constructor(embark, options){ this.embark = embark; this.options = options; } - build(contract){ + build(contract){ + const filename = contract.className.toLowerCase() + '.html'; + const filePath = './app/' + filename; + if (fs.existsSync(filePath)){ + throw new Error("file '" + filePath + "' already exists"); + } + + const templatePath = fs.embarkPath('lib/modules/scaffolding-react/templates/index.tpl'); + const source = fs.readFileSync(templatePath).toString(); + const template = Handlebars.compile(source); + + let data = { + 'title': contract.className + }; + + // Write template + const result = template(data); + fs.writeFileSync(filePath, result); + + return "File '" + filePath + "' created successfully"; } } - module.exports = ScaffoldingReact; diff --git a/lib/modules/scaffolding-react/templates/index.tpl b/lib/modules/scaffolding-react/templates/index.tpl new file mode 100644 index 00000000..3fffecef --- /dev/null +++ b/lib/modules/scaffolding-react/templates/index.tpl @@ -0,0 +1,12 @@ + + + + {{title}} + + + + + + + + \ No newline at end of file diff --git a/lib/modules/scaffolding/index.js b/lib/modules/scaffolding/index.js index 1bf099bf..67797847 100644 --- a/lib/modules/scaffolding/index.js +++ b/lib/modules/scaffolding/index.js @@ -2,7 +2,7 @@ const fs = require('fs'); const commandName = "generate-ui"; -const formatReplyMsg = (message) => commandName + ": " + message; +const errorMessage = (message) => new Error(commandName + ": " + message); class Scaffolding { constructor(embark, options){ @@ -12,15 +12,6 @@ class Scaffolding { 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; } @@ -34,26 +25,25 @@ class Scaffolding { } else { let plugins = this.embark.plugins.getPluginsFor(this.framework); if(plugins.length !== 1){ - return formatReplyMsg("Could not find plugin for framework '" + this.framework + "'"); + throw errorMessage("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; + + if(!this.isContract(contractName)){ + return errorMessage("contract '" + contractName + "' does not exist"); } - return formatReplyMsg("done!"); + 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); + } } } diff --git a/package.json b/package.json index ff6c94ef..0c9ca437 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,11 @@ "async": "^2.0.1", "babel-core": "^6.26.0", "babel-loader": "^7.1.2", + "babel-plugin-webpack-aliases": "^1.1.3", "babel-preset-es2015": "^6.24.1", "babel-preset-es2016": "^6.24.1", "babel-preset-es2017": "6.24.1", "babel-preset-react": "^6.24.1", - "babel-plugin-webpack-aliases": "^1.1.3", "blessed": "^0.1.81", "chokidar": "^1.6.0", "colors": "^1.1.2", @@ -41,6 +41,7 @@ "follow-redirects": "^1.2.4", "fs-extra": "^2.0.0", "globule": "^1.1.0", + "handlebars": "^4.0.11", "http-shutdown": "^1.2.0", "ipfs-api": "17.2.4", "merge": "^1.2.0",