2018-05-09 21:02:17 +00:00
|
|
|
class Scaffolding {
|
2018-10-05 23:13:00 +00:00
|
|
|
constructor(engine, _options){
|
|
|
|
this.engine = engine;
|
|
|
|
this.options = _options;
|
|
|
|
this.plugins = _options.plugins;
|
|
|
|
|
2018-10-12 17:50:35 +00:00
|
|
|
engine.events.setCommandHandler("scaffolding:generate:contract", (options, cb) => {
|
|
|
|
this.framework = options.contractLanguage;
|
|
|
|
this.fields = options.fields;
|
|
|
|
this.generate(options.contract, options.overwrite, true, cb);
|
2018-10-05 23:13:00 +00:00
|
|
|
});
|
2018-10-12 18:23:31 +00:00
|
|
|
|
|
|
|
engine.events.setCommandHandler("scaffolding:generate:ui", (options, cb) => {
|
|
|
|
this.framework = options.framework;
|
|
|
|
this.fields = options.fields;
|
|
|
|
this.generate(options.contract, options.overwrite, false, cb);
|
|
|
|
});
|
2018-05-09 21:02:17 +00:00
|
|
|
}
|
2018-10-12 19:07:59 +00:00
|
|
|
|
2018-10-12 17:50:35 +00:00
|
|
|
getScaffoldPlugin(framework){
|
2018-10-05 23:13:00 +00:00
|
|
|
let dappGenerators = this.plugins.getPluginsFor('dappGenerator');
|
2018-10-12 17:50:35 +00:00
|
|
|
let builder;
|
2018-10-05 23:13:00 +00:00
|
|
|
dappGenerators.forEach((plugin) => {
|
|
|
|
plugin.dappGenerators.forEach((d) => {
|
2018-10-12 17:50:35 +00:00
|
|
|
if(d.framework === framework){
|
|
|
|
builder = d.cb;
|
2018-10-05 23:13:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 17:50:35 +00:00
|
|
|
return builder;
|
|
|
|
}
|
2018-10-05 23:13:00 +00:00
|
|
|
|
2018-10-12 18:23:31 +00:00
|
|
|
loadFrameworkModule(){
|
2018-10-12 17:50:35 +00:00
|
|
|
switch(this.framework){
|
|
|
|
case 'react':
|
|
|
|
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
|
|
|
break;
|
|
|
|
case 'solidity':
|
|
|
|
this.plugins.loadInternalPlugin('scaffolding-solidity', this.options);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
2018-10-12 18:23:31 +00:00
|
|
|
}
|
2018-10-12 17:50:35 +00:00
|
|
|
|
2018-10-12 18:23:31 +00:00
|
|
|
generate(contractName, overwrite, isContractGeneration, cb){
|
|
|
|
this.loadFrameworkModule();
|
2018-10-12 17:50:35 +00:00
|
|
|
|
2018-10-12 19:07:59 +00:00
|
|
|
const build = this.getScaffoldPlugin(this.framework);
|
2018-10-12 17:50:35 +00:00
|
|
|
if(!build){
|
2018-10-05 23:13:00 +00:00
|
|
|
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
2018-10-12 17:50:35 +00:00
|
|
|
process.exit();
|
2018-10-12 18:23:31 +00:00
|
|
|
}
|
2018-10-12 19:07:59 +00:00
|
|
|
|
|
|
|
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
|
2018-10-05 23:13:00 +00:00
|
|
|
cb();
|
2018-10-12 18:23:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let contract;
|
2018-10-12 19:07:59 +00:00
|
|
|
if(isContractGeneration && hasFields){
|
2018-10-12 18:23:31 +00:00
|
|
|
contract = {className: contractName, fields: this.fields};
|
|
|
|
try {
|
|
|
|
build(contract, overwrite, cb);
|
|
|
|
} catch(err){
|
|
|
|
this.engine.logger.error(err.message);
|
|
|
|
}
|
2018-10-05 23:13:00 +00:00
|
|
|
} else {
|
2018-10-12 18:23:31 +00:00
|
|
|
// Contract already exists
|
2018-10-05 23:13:00 +00:00
|
|
|
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
|
|
|
if(_err) throw new Error(_err);
|
|
|
|
const contract = contractsList.find(x => x.className === contractName);
|
2018-10-12 19:07:59 +00:00
|
|
|
if(!contract){
|
|
|
|
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
|
|
|
cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-05 23:13:00 +00:00
|
|
|
try {
|
|
|
|
build(contract, overwrite, cb);
|
|
|
|
} catch(err){
|
|
|
|
this.engine.logger.error(err.message);
|
|
|
|
}
|
|
|
|
});
|
2018-05-09 21:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Scaffolding;
|