From c4961d69b6015039364866f2a8319e0d05efd8f4 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 10 May 2018 12:16:13 -0400 Subject: [PATCH] JS generation + embark.json configuration update --- lib/modules/scaffolding-react/index.js | 38 +++++++++++++------ .../scaffolding-react/templates/dapp.js.tpl | 9 +++++ .../templates/index.html.tpl | 10 +++++ .../scaffolding-react/templates/index.tpl | 12 ------ 4 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 lib/modules/scaffolding-react/templates/dapp.js.tpl create mode 100644 lib/modules/scaffolding-react/templates/index.html.tpl delete mode 100644 lib/modules/scaffolding-react/templates/index.tpl diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 6c7d223b9..9ba57a0ed 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -1,35 +1,51 @@ 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){ - const filename = contract.className.toLowerCase() + '.html'; + _generateFile(contract, templateFilename, extension, data){ + const filename = contract.className.toLowerCase() + '.' + extension; const filePath = './app/' + filename; - if (fs.existsSync(filePath)){ - throw new Error("file '" + filePath + "' already exists"); + // throw new Error("file '" + filePath + "' already exists"); } - const templatePath = fs.embarkPath('lib/modules/scaffolding-react/templates/index.tpl'); + const templatePath = fs.embarkPath('lib/modules/scaffolding-react/templates/' + templateFilename); 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); + } + + build(contract){ + const filename = contract.className.toLowerCase(); + + this._generateFile(contract, 'index.html.tpl', 'html', + { + 'title': contract.className, + 'filename': filename + }); + + this._generateFile(contract, 'dapp.js.tpl', 'js', {}); + + // 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)); + + - return "File '" + filePath + "' created successfully"; } } diff --git a/lib/modules/scaffolding-react/templates/dapp.js.tpl b/lib/modules/scaffolding-react/templates/dapp.js.tpl new file mode 100644 index 000000000..c6b8f74ae --- /dev/null +++ b/lib/modules/scaffolding-react/templates/dapp.js.tpl @@ -0,0 +1,9 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import { Tabs, Tab } from 'react-bootstrap'; + +ReactDOM.render(
+ ABCDE +
, + document.getElementById('app') +); \ No newline at end of file diff --git a/lib/modules/scaffolding-react/templates/index.html.tpl b/lib/modules/scaffolding-react/templates/index.html.tpl new file mode 100644 index 000000000..ea4bb2aac --- /dev/null +++ b/lib/modules/scaffolding-react/templates/index.html.tpl @@ -0,0 +1,10 @@ + + + + {{title}} + + +
+ + + \ No newline at end of file diff --git a/lib/modules/scaffolding-react/templates/index.tpl b/lib/modules/scaffolding-react/templates/index.tpl deleted file mode 100644 index 3fffecef3..000000000 --- a/lib/modules/scaffolding-react/templates/index.tpl +++ /dev/null @@ -1,12 +0,0 @@ - - - - {{title}} - - - - - - - - \ No newline at end of file