JS generation + embark.json configuration update

This commit is contained in:
Richard Ramos 2018-05-10 12:16:13 -04:00 committed by Pascal Precht
parent d1dd890407
commit c4961d69b6
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
4 changed files with 46 additions and 23 deletions

View File

@ -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";
}
}

View File

@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Tabs, Tab } from 'react-bootstrap';
ReactDOM.render(<div>
ABCDE
</div>,
document.getElementById('app')
);

View File

@ -0,0 +1,10 @@
<!doctype html>
<html lang="en" dir="ltr">
<head>
<title>{{title}}</title>
</head>
<body class="container">
<div id="app"></div>
<script src="js/{{filename}}.js"></script>
</body>
</html>

View File

@ -1,12 +0,0 @@
<!doctype html>
<html lang="en" dir="ltr">
<head>
<title>{{title}}</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
</body>
</html>