embark-area-51/lib/modules/scaffolding-react/index.js

101 lines
3.2 KiB
JavaScript
Raw Normal View History

const Handlebars = require('handlebars');
const fs = require('../../core/fs');
Handlebars.registerHelper('capitalize', function(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
});
Handlebars.registerHelper('ifview', function(stateMutability, options) {
2018-08-02 19:17:40 +00:00
let result = stateMutability === 'view' || stateMutability === 'pure' || stateMutability === 'constant';
if (result) {
return options.fn(this);
}
return options.inverse(this);
});
2018-05-11 13:26:27 +00:00
Handlebars.registerHelper('ifeq', function(elem, value, options){
2018-08-02 19:17:40 +00:00
if (elem === value) {
2018-05-11 13:26:27 +00:00
return options.fn(this);
}
return options.inverse(this);
2018-05-11 13:26:27 +00:00
});
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
if (arr.length > val) {
return options.fn(this);
}
return options.inverse(this);
});
Handlebars.registerHelper('emptyname', function(name, index) {
2018-05-11 13:26:27 +00:00
return name ? name : 'output' + index;
});
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
2018-08-02 19:17:40 +00:00
let funCount = abiDefinition.filter(x => x.name === functionName).length;
if(funCount === 1){
return '.' + functionName;
}
return new Handlebars.SafeString(`['${functionName}(${inputs !== null ? inputs.map(input => input.type).join(',') : ''})']`);
});
class ScaffoldingReact {
constructor(embark, options){
this.embark = embark;
this.options = options;
2018-05-11 19:49:06 +00:00
this.embark.registerDappGenerator('react', this.build.bind(this));
}
_generateFile(contract, templateFilename, extension, data){
const filename = contract.className.toLowerCase() + '.' + extension;
const filePath = './app/' + filename;
if (fs.existsSync(filePath)){
2018-05-11 20:14:53 +00:00
throw new Error("file '" + filePath + "' already exists");
}
const templatePath = fs.embarkPath('lib/modules/scaffolding-react/templates/' + templateFilename);
const source = fs.readFileSync(templatePath).toString();
const template = Handlebars.compile(source);
// Write template
const result = template(data);
fs.writeFileSync(filePath, result);
}
2018-05-10 16:31:10 +00:00
_buildHTML(contract){
const filename = contract.className.toLowerCase();
this._generateFile(contract, 'index.html.tpl', 'html',
2018-05-10 16:31:10 +00:00
{
'title': contract.className,
'filename': filename
});
}
2018-08-02 19:17:40 +00:00
build(contract){
2018-05-10 16:31:10 +00:00
this._buildHTML(contract);
const filename = contract.className.toLowerCase();
2018-05-10 16:31:10 +00:00
this._generateFile(contract, 'dapp.js.tpl', 'js',
{
2018-05-11 13:26:27 +00:00
'title': contract.className,
'contractName': contract.className,
2018-08-02 19:17:40 +00:00
'functions': contract.abiDefinition.filter(x => x.type === 'function')
2018-05-10 16:31:10 +00:00
});
// 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-11 19:49:06 +00:00
this.embark.logger.info(filename + ".html generated");
}
}
module.exports = ScaffoldingReact;