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');
|
2018-10-05 20:54:02 +00:00
|
|
|
let utils = require('../../utils/utils.js');
|
|
|
|
|
2018-05-10 16:16:13 +00:00
|
|
|
|
2018-05-10 21:02:47 +00:00
|
|
|
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';
|
2018-05-10 21:02:47 +00:00
|
|
|
if (result) {
|
|
|
|
return options.fn(this);
|
2018-05-15 20:19:39 +00:00
|
|
|
}
|
|
|
|
return options.inverse(this);
|
2018-05-10 21:02:47 +00:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
}
|
2018-05-15 20:19:39 +00:00
|
|
|
return options.inverse(this);
|
2018-05-11 13:26:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
|
2018-05-10 21:02:47 +00:00
|
|
|
if (arr.length > val) {
|
|
|
|
return options.fn(this);
|
2018-05-15 20:19:39 +00:00
|
|
|
}
|
|
|
|
return options.inverse(this);
|
2018-05-10 21:02:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Handlebars.registerHelper('emptyname', function(name, index) {
|
2018-05-11 13:26:27 +00:00
|
|
|
return name ? name : 'output' + index;
|
2018-05-10 21:02:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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){
|
2018-05-10 21:02:47 +00:00
|
|
|
return '.' + functionName;
|
|
|
|
}
|
2018-05-15 20:19:39 +00:00
|
|
|
return new Handlebars.SafeString(`['${functionName}(${inputs !== null ? inputs.map(input => input.type).join(',') : ''})']`);
|
2018-05-10 21:02:47 +00:00
|
|
|
});
|
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-11 19:49:06 +00:00
|
|
|
this.embark.registerDappGenerator('react', this.build.bind(this));
|
2018-05-09 21:02:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:45:03 +00:00
|
|
|
_generateFile(contract, templateFilename, extension, data, overwrite){
|
2018-05-10 16:16:13 +00:00
|
|
|
const filename = contract.className.toLowerCase() + '.' + extension;
|
2018-05-10 15:25:31 +00:00
|
|
|
const filePath = './app/' + filename;
|
2018-10-05 15:45:03 +00:00
|
|
|
if (!overwrite && fs.existsSync(filePath)){
|
2018-05-11 20:14:53 +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
|
|
|
}
|
|
|
|
|
2018-10-05 20:54:02 +00:00
|
|
|
build(contract, overwrite, cb){
|
|
|
|
const packageInstallCmd = 'npm install react react-bootstrap react-dom';
|
|
|
|
utils.runCmd(packageInstallCmd, null, (err) => {
|
|
|
|
if (err) {
|
|
|
|
this.embark.logger.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-10-05 23:13:00 +00:00
|
|
|
try {
|
|
|
|
const filename = contract.className.toLowerCase();
|
|
|
|
|
|
|
|
this._generateFile(contract, 'index.html.tpl', 'html',
|
|
|
|
{
|
|
|
|
'title': contract.className,
|
|
|
|
filename
|
|
|
|
}, overwrite);
|
|
|
|
|
|
|
|
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
|
|
|
{
|
|
|
|
'title': contract.className,
|
|
|
|
'contractName': contract.className,
|
|
|
|
'functions': contract.abiDefinition.filter(x => x.type === 'function')
|
|
|
|
}, overwrite);
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
this.embark.logger.info(filename + ".html generated");
|
|
|
|
this.embark.logger.info(filename + ".js generated");
|
|
|
|
|
|
|
|
} catch(error){
|
|
|
|
this.embark.logger.error(error.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-10-05 20:54:02 +00:00
|
|
|
cb();
|
|
|
|
});
|
2018-05-09 21:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ScaffoldingReact;
|