2018-05-09 21:02:17 +00:00
|
|
|
|
2018-05-10 15:25:31 +00:00
|
|
|
const Handlebars = require('handlebars');
|
|
|
|
const fs = require('../../core/fs');
|
|
|
|
const utils = require('../../utils/utils');
|
2018-05-10 16:16:13 +00:00
|
|
|
|
|
|
|
|
2018-05-09 21:02:17 +00:00
|
|
|
class ScaffoldingReact {
|
|
|
|
constructor(embark, options){
|
|
|
|
this.embark = embark;
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
2018-05-10 16:16:13 +00:00
|
|
|
_generateFile(contract, templateFilename, extension, data){
|
|
|
|
const filename = contract.className.toLowerCase() + '.' + extension;
|
2018-05-10 15:25:31 +00:00
|
|
|
const filePath = './app/' + filename;
|
|
|
|
if (fs.existsSync(filePath)){
|
2018-05-10 16:16:13 +00:00
|
|
|
// throw new Error("file '" + filePath + "' already exists");
|
2018-05-10 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 16:16:13 +00:00
|
|
|
const templatePath = fs.embarkPath('lib/modules/scaffolding-react/templates/' + templateFilename);
|
2018-05-10 15:25:31 +00:00
|
|
|
const source = fs.readFileSync(templatePath).toString();
|
|
|
|
const template = Handlebars.compile(source);
|
2018-05-09 21:02:17 +00:00
|
|
|
|
2018-05-10 15:25:31 +00:00
|
|
|
// Write template
|
|
|
|
const result = template(data);
|
|
|
|
fs.writeFileSync(filePath, result);
|
2018-05-10 16:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
2018-05-10 15:25:31 +00:00
|
|
|
|
2018-05-09 21:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScaffoldingReact;
|