embark/lib/modules/scaffolding/index.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

class Scaffolding {
constructor(embark, _options) {
this.embark = embark;
2018-10-17 17:16:28 +00:00
this.options = _options;
this.plugins = _options.plugins;
2018-10-05 23:13:00 +00:00
embark.events.setCommandHandler("scaffolding:generate:contract", (options, cb) => {
2018-10-17 17:16:28 +00:00
this.framework = options.contractLanguage;
this.fields = options.fields;
this.generate(options.contract, options.overwrite, true, cb);
});
embark.events.setCommandHandler("scaffolding:generate:ui", (options, cb) => {
2018-10-17 17:16:28 +00:00
this.framework = options.framework;
this.fields = options.fields;
this.generate(options.contract, options.overwrite, false, cb);
});
}
2018-10-05 23:13:00 +00:00
2018-10-17 17:16:28 +00:00
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;
}
2018-10-17 17:16:28 +00:00
});
});
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:
this.embark.logger.error(__('Selected framework not supported'));
this.embark.logger.error(__('Supported Frameworks are: %s', 'react, solidity'));
process.exit(1);
}
2018-10-17 17:16:28 +00:00
}
2018-10-17 17:16:28 +00:00
generate(contractName, overwrite, isContractGeneration, cb) {
this.loadFrameworkModule();
2018-10-17 17:16:28 +00:00
const build = this.getScaffoldPlugin(this.framework);
if (!build) {
this.embark.logger.error("Could not find plugin for framework '" + this.framework + "'");
process.exit(1);
2018-10-17 17:16:28 +00:00
}
const hasFields = Object.getOwnPropertyNames(this.fields).length !== 0;
2018-10-12 19:07:59 +00:00
2018-10-17 17:16:28 +00:00
if (isContractGeneration && !hasFields) {
// This happens when you execute "scaffold ContractName",
// assuming the contract already exists in a .sol file
cb();
return;
}
2018-10-12 19:07:59 +00:00
2018-10-17 17:16:28 +00:00
let contract;
if (isContractGeneration && hasFields) {
contract = {className: contractName, fields: this.fields};
try {
build(contract, overwrite, cb);
} catch (err) {
this.embark.logger.error(err.message);
2018-10-17 17:16:28 +00:00
}
} else {
// Contract already exists
this.embark.events.request("contracts:list", (_err, contractsList) => {
2018-10-17 17:16:28 +00:00
if (_err) throw new Error(_err);
const contract = contractsList.find(x => x.className === contractName);
if (!contract) {
this.embark.logger.error("contract '" + contractName + "' does not exist");
2018-10-17 17:16:28 +00:00
cb();
return;
}
2018-10-17 17:16:28 +00:00
try {
build(contract, overwrite, cb);
} catch (err) {
this.embark.logger.error(err.message);
}
2018-10-17 17:16:28 +00:00
});
}
2018-10-17 17:16:28 +00:00
}
}
module.exports = Scaffolding;