mirror of https://github.com/embarklabs/embark.git
Validation of datatypes + small refactoring
This commit is contained in:
parent
d6de374ce7
commit
939642f74c
15
cmd/cmd.js
15
cmd/cmd.js
|
@ -339,9 +339,22 @@ class Cmd {
|
|||
|
||||
const fieldMapping = {};
|
||||
if(fields.length > 0){
|
||||
// TODO: validate fields
|
||||
const typeRegex = /^(u?int[0-9]{0,3}(\[\])?|string|bool|address|bytes[0-9]{0,3})(\[\])?$/;
|
||||
const varRegex = /^[a-zA-Z][a-zA-Z0-9_]*$/;
|
||||
|
||||
fields.forEach(curr => {
|
||||
const c = curr.split(':');
|
||||
|
||||
if(!varRegex.test(c[0])){
|
||||
console.log("Invalid variable name: " + c[0]);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if(!typeRegex.test(c[1])){
|
||||
console.log("Invalid datatype: " + c[1] + " - The dApp generator might not support this type at the moment");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
fieldMapping[c[0]] = c[1];
|
||||
});
|
||||
}
|
||||
|
|
|
@ -490,8 +490,7 @@ class EmbarkController {
|
|||
});
|
||||
},
|
||||
function generateUI(callback){
|
||||
engine.events.request("scaffolding:generate", options, () => {
|
||||
|
||||
engine.events.request("scaffolding:generate:ui", options, () => {
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -98,8 +98,8 @@ class ScaffoldingReact {
|
|||
|
||||
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
||||
|
||||
this.embark.logger.info(filename + ".html generated");
|
||||
this.embark.logger.info(filename + ".js generated");
|
||||
this.embark.logger.info('app/' + filename + ".html generated");
|
||||
this.embark.logger.info('app/' + filename + ".js generated");
|
||||
|
||||
} catch(error){
|
||||
this.embark.logger.error(error.message);
|
||||
|
|
|
@ -25,22 +25,21 @@ class ScaffoldingSolidity {
|
|||
fs.writeFileSync(filePath, result);
|
||||
}
|
||||
|
||||
build(contractDetails, overwrite, cb){
|
||||
const {contract, fields} = contractDetails;
|
||||
build(contract, overwrite, cb){
|
||||
try {
|
||||
const filename = contract.className.toLowerCase();
|
||||
|
||||
this._generateFile(contract, 'contract.sol.tpl', 'sol',
|
||||
{
|
||||
this._generateFile(contract, 'contract.sol.tpl', 'sol', {
|
||||
'contractName': contract.className,
|
||||
'structName': contract.className + "Struct",
|
||||
'fields': Object.keys(fields).map(f => { return {name:f, type:fields[f]}; })
|
||||
}, overwrite);
|
||||
'fields': Object.keys(contract.fields).map(f => {
|
||||
return {name:f, type: contract.fields[f]};
|
||||
})
|
||||
}, overwrite);
|
||||
this.embark.logger.info("contracts/" + filename + ".sol generated");
|
||||
|
||||
cb();
|
||||
|
||||
} catch(error){
|
||||
} catch(error) {
|
||||
this.embark.logger.error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
|
@ -4,17 +4,17 @@ class Scaffolding {
|
|||
this.options = _options;
|
||||
this.plugins = _options.plugins;
|
||||
|
||||
engine.events.setCommandHandler("scaffolding:generate", (options, cb) => {
|
||||
this.framework = options.framework;
|
||||
this.fields = options.fields;
|
||||
this.generate(options.contract, options.overwrite, false, 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);
|
||||
});
|
||||
}
|
||||
|
||||
isContract(contractName){
|
||||
|
@ -34,8 +34,7 @@ class Scaffolding {
|
|||
return builder;
|
||||
}
|
||||
|
||||
generate(contractName, overwrite, preDeployment, cb){
|
||||
|
||||
loadFrameworkModule(){
|
||||
switch(this.framework){
|
||||
case 'react':
|
||||
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||
|
@ -45,21 +44,34 @@ class Scaffolding {
|
|||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
const fields = this.fields;
|
||||
generate(contractName, overwrite, isContractGeneration, cb){
|
||||
this.loadFrameworkModule();
|
||||
|
||||
let build = this.getScaffoldPlugin(this.framework);
|
||||
if(!build){
|
||||
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
||||
process.exit();
|
||||
cb();
|
||||
} else if(!this.isContract(contractName) && Object.getOwnPropertyNames(this.fields).length === 0){
|
||||
}
|
||||
|
||||
if(!this.isContract(contractName) && Object.getOwnPropertyNames(this.fields).length === 0){
|
||||
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
||||
cb();
|
||||
} else if(preDeployment) {
|
||||
build({contract: {className: contractName}, fields}, overwrite, cb);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let contract;
|
||||
if(isContractGeneration){
|
||||
contract = {className: contractName, fields: this.fields};
|
||||
try {
|
||||
build(contract, overwrite, cb);
|
||||
} catch(err){
|
||||
this.engine.logger.error(err.message);
|
||||
}
|
||||
} else {
|
||||
// Contract exists
|
||||
// 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);
|
||||
|
|
Loading…
Reference in New Issue