mirror of https://github.com/embarklabs/embark.git
fix indentation
This commit is contained in:
parent
9a41e58679
commit
2205a26236
|
@ -1,125 +1,124 @@
|
|||
|
||||
const Handlebars = require('handlebars');
|
||||
const fs = require('../../core/fs');
|
||||
let utils = require('../../utils/utils.js');
|
||||
|
||||
|
||||
Handlebars.registerHelper('capitalize', function(word) {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifview', function(stateMutability, options) {
|
||||
let result = stateMutability === 'view' || stateMutability === 'pure' || stateMutability === 'constant';
|
||||
if (result) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
let result = stateMutability === 'view' || stateMutability === 'pure' || stateMutability === 'constant';
|
||||
if (result) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifeq', function(elem, value, options){
|
||||
if (elem === value) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
Handlebars.registerHelper('ifeq', function(elem, value, options) {
|
||||
if (elem === value) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifarr', function(elem, options){
|
||||
if(elem.indexOf('[]') > -1){
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
Handlebars.registerHelper('ifarr', function(elem, options) {
|
||||
if (elem.indexOf('[]') > -1) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
|
||||
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
|
||||
if (arr.length > val) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
if (arr.length > val) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('emptyname', function(name, index) {
|
||||
return name ? name : 'output' + index;
|
||||
return name ? name : 'output' + index;
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('trim', function(name) {
|
||||
return name.replace('[]', '');
|
||||
return name.replace('[]', '');
|
||||
});
|
||||
|
||||
|
||||
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
|
||||
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(',') : ''})']`);
|
||||
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs) {
|
||||
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;
|
||||
this.embark.registerDappGenerator('react', this.build.bind(this));
|
||||
constructor(embark, options) {
|
||||
this.embark = embark;
|
||||
this.options = options;
|
||||
this.embark.registerDappGenerator('react', this.build.bind(this));
|
||||
}
|
||||
|
||||
_generateFile(contract, templateFilename, extension, data, overwrite) {
|
||||
const filename = contract.className.toLowerCase() + '.' + extension;
|
||||
const filePath = './app/' + filename;
|
||||
if (!overwrite && fs.existsSync(filePath)) {
|
||||
throw new Error("file '" + filePath + "' already exists");
|
||||
}
|
||||
|
||||
_generateFile(contract, templateFilename, extension, data, overwrite){
|
||||
const filename = contract.className.toLowerCase() + '.' + extension;
|
||||
const filePath = './app/' + filename;
|
||||
if (!overwrite && fs.existsSync(filePath)){
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Write template
|
||||
const result = template(data);
|
||||
fs.writeFileSync(filePath, result);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
try {
|
||||
const filename = contract.className.toLowerCase();
|
||||
|
||||
try {
|
||||
const filename = contract.className.toLowerCase();
|
||||
this._generateFile(contract, 'index.html.tpl', 'html',
|
||||
{
|
||||
'title': contract.className,
|
||||
filename
|
||||
}, overwrite);
|
||||
|
||||
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('app/' + filename + ".html generated");
|
||||
this.embark.logger.info('app/' + filename + ".js generated");
|
||||
|
||||
} catch(error){
|
||||
this.embark.logger.error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
cb();
|
||||
});
|
||||
}
|
||||
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('app/' + filename + ".html generated");
|
||||
this.embark.logger.info('app/' + filename + ".js generated");
|
||||
|
||||
} catch (error) {
|
||||
this.embark.logger.error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
cb();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ScaffoldingReact;
|
||||
|
|
|
@ -1,92 +1,92 @@
|
|||
class Scaffolding {
|
||||
constructor(engine, _options){
|
||||
this.engine = engine;
|
||||
this.options = _options;
|
||||
this.plugins = _options.plugins;
|
||||
constructor(engine, _options) {
|
||||
this.engine = engine;
|
||||
this.options = _options;
|
||||
this.plugins = _options.plugins;
|
||||
|
||||
engine.events.setCommandHandler("scaffolding:generate:contract", (options, cb) => {
|
||||
this.framework = options.contractLanguage;
|
||||
this.fields = options.fields;
|
||||
this.generate(options.contract, options.overwrite, true, cb);
|
||||
});
|
||||
engine.events.setCommandHandler("scaffolding:generate:contract", (options, cb) => {
|
||||
this.framework = options.contractLanguage;
|
||||
this.fields = options.fields;
|
||||
this.generate(options.contract, options.overwrite, true, cb);
|
||||
});
|
||||
|
||||
engine.events.setCommandHandler("scaffolding:generate:ui", (options, cb) => {
|
||||
this.framework = options.framework;
|
||||
this.fields = options.fields;
|
||||
this.generate(options.contract, options.overwrite, false, cb);
|
||||
});
|
||||
engine.events.setCommandHandler("scaffolding:generate:ui", (options, cb) => {
|
||||
this.framework = options.framework;
|
||||
this.fields = options.fields;
|
||||
this.generate(options.contract, options.overwrite, false, cb);
|
||||
});
|
||||
}
|
||||
|
||||
getScaffoldPlugin(framework) {
|
||||
let dappGenerators = this.plugins.getPluginsFor('dappGenerator');
|
||||
let builder = null;
|
||||
dappGenerators.forEach((plugin) => {
|
||||
plugin.dappGenerators.forEach((d) => {
|
||||
if (d.framework === framework) {
|
||||
builder = d.cb;
|
||||
}
|
||||
});
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
|
||||
loadFrameworkModule() {
|
||||
switch (this.framework) {
|
||||
case 'react':
|
||||
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||
break;
|
||||
case 'solidity':
|
||||
this.plugins.loadInternalPlugin('scaffolding-solidity', this.options);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
getScaffoldPlugin(framework){
|
||||
let dappGenerators = this.plugins.getPluginsFor('dappGenerator');
|
||||
let builder;
|
||||
dappGenerators.forEach((plugin) => {
|
||||
plugin.dappGenerators.forEach((d) => {
|
||||
if(d.framework === framework){
|
||||
builder = d.cb;
|
||||
}
|
||||
});
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
|
||||
generate(contractName, overwrite, isContractGeneration, cb) {
|
||||
this.loadFrameworkModule();
|
||||
|
||||
const build = this.getScaffoldPlugin(this.framework);
|
||||
if (!build) {
|
||||
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
||||
process.exit();
|
||||
}
|
||||
|
||||
loadFrameworkModule(){
|
||||
switch(this.framework){
|
||||
case 'react':
|
||||
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||
break;
|
||||
case 'solidity':
|
||||
this.plugins.loadInternalPlugin('scaffolding-solidity', this.options);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
const hasFields = Object.getOwnPropertyNames(this.fields).length !== 0;
|
||||
|
||||
if (isContractGeneration && !hasFields) {
|
||||
// This happens when you execute "scaffold ContractName",
|
||||
// assuming the contract already exists in a .sol file
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
generate(contractName, overwrite, isContractGeneration, cb){
|
||||
this.loadFrameworkModule();
|
||||
|
||||
const build = this.getScaffoldPlugin(this.framework);
|
||||
if(!build){
|
||||
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
||||
process.exit();
|
||||
let contract;
|
||||
if (isContractGeneration && hasFields) {
|
||||
contract = {className: contractName, fields: this.fields};
|
||||
try {
|
||||
build(contract, overwrite, cb);
|
||||
} catch (err) {
|
||||
this.engine.logger.error(err.message);
|
||||
}
|
||||
} else {
|
||||
// Contract already exists
|
||||
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
||||
if (_err) throw new Error(_err);
|
||||
const contract = contractsList.find(x => x.className === contractName);
|
||||
if (!contract) {
|
||||
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
const hasFields = Object.getOwnPropertyNames(this.fields).length !== 0;
|
||||
|
||||
if(isContractGeneration && !hasFields){
|
||||
// This happens when you execute "scaffold ContractName",
|
||||
// assuming the contract already exists in a .sol file
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
let contract;
|
||||
if(isContractGeneration && hasFields){
|
||||
contract = {className: contractName, fields: this.fields};
|
||||
try {
|
||||
build(contract, overwrite, cb);
|
||||
} catch(err){
|
||||
this.engine.logger.error(err.message);
|
||||
}
|
||||
} else {
|
||||
// Contract already exists
|
||||
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
||||
if(_err) throw new Error(_err);
|
||||
const contract = contractsList.find(x => x.className === contractName);
|
||||
if(!contract){
|
||||
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
build(contract, overwrite, cb);
|
||||
} catch(err){
|
||||
this.engine.logger.error(err.message);
|
||||
}
|
||||
});
|
||||
try {
|
||||
build(contract, overwrite, cb);
|
||||
} catch (err) {
|
||||
this.engine.logger.error(err.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue