mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-18 16:46:38 +00:00
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 = {};
|
const fieldMapping = {};
|
||||||
if(fields.length > 0){
|
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 => {
|
fields.forEach(curr => {
|
||||||
const c = curr.split(':');
|
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];
|
fieldMapping[c[0]] = c[1];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -490,8 +490,7 @@ class EmbarkController {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
function generateUI(callback){
|
function generateUI(callback){
|
||||||
engine.events.request("scaffolding:generate", options, () => {
|
engine.events.request("scaffolding:generate:ui", options, () => {
|
||||||
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -98,8 +98,8 @@ class ScaffoldingReact {
|
|||||||
|
|
||||||
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
||||||
|
|
||||||
this.embark.logger.info(filename + ".html generated");
|
this.embark.logger.info('app/' + filename + ".html generated");
|
||||||
this.embark.logger.info(filename + ".js generated");
|
this.embark.logger.info('app/' + filename + ".js generated");
|
||||||
|
|
||||||
} catch(error){
|
} catch(error){
|
||||||
this.embark.logger.error(error.message);
|
this.embark.logger.error(error.message);
|
||||||
|
@ -25,22 +25,21 @@ class ScaffoldingSolidity {
|
|||||||
fs.writeFileSync(filePath, result);
|
fs.writeFileSync(filePath, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
build(contractDetails, overwrite, cb){
|
build(contract, overwrite, cb){
|
||||||
const {contract, fields} = contractDetails;
|
|
||||||
try {
|
try {
|
||||||
const filename = contract.className.toLowerCase();
|
const filename = contract.className.toLowerCase();
|
||||||
|
|
||||||
this._generateFile(contract, 'contract.sol.tpl', 'sol',
|
this._generateFile(contract, 'contract.sol.tpl', 'sol', {
|
||||||
{
|
|
||||||
'contractName': contract.className,
|
'contractName': contract.className,
|
||||||
'structName': contract.className + "Struct",
|
'structName': contract.className + "Struct",
|
||||||
'fields': Object.keys(fields).map(f => { return {name:f, type:fields[f]}; })
|
'fields': Object.keys(contract.fields).map(f => {
|
||||||
}, overwrite);
|
return {name:f, type: contract.fields[f]};
|
||||||
|
})
|
||||||
|
}, overwrite);
|
||||||
this.embark.logger.info("contracts/" + filename + ".sol generated");
|
this.embark.logger.info("contracts/" + filename + ".sol generated");
|
||||||
|
|
||||||
cb();
|
cb();
|
||||||
|
} catch(error) {
|
||||||
} catch(error){
|
|
||||||
this.embark.logger.error(error.message);
|
this.embark.logger.error(error.message);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,17 @@ class Scaffolding {
|
|||||||
this.options = _options;
|
this.options = _options;
|
||||||
this.plugins = _options.plugins;
|
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) => {
|
engine.events.setCommandHandler("scaffolding:generate:contract", (options, cb) => {
|
||||||
this.framework = options.contractLanguage;
|
this.framework = options.contractLanguage;
|
||||||
this.fields = options.fields;
|
this.fields = options.fields;
|
||||||
this.generate(options.contract, options.overwrite, true, cb);
|
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){
|
isContract(contractName){
|
||||||
@ -34,8 +34,7 @@ class Scaffolding {
|
|||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
generate(contractName, overwrite, preDeployment, cb){
|
loadFrameworkModule(){
|
||||||
|
|
||||||
switch(this.framework){
|
switch(this.framework){
|
||||||
case 'react':
|
case 'react':
|
||||||
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
this.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||||
@ -45,21 +44,34 @@ class Scaffolding {
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const fields = this.fields;
|
generate(contractName, overwrite, isContractGeneration, cb){
|
||||||
|
this.loadFrameworkModule();
|
||||||
|
|
||||||
let build = this.getScaffoldPlugin(this.framework);
|
let build = this.getScaffoldPlugin(this.framework);
|
||||||
if(!build){
|
if(!build){
|
||||||
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
||||||
process.exit();
|
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");
|
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
||||||
cb();
|
cb();
|
||||||
} else if(preDeployment) {
|
return;
|
||||||
build({contract: {className: contractName}, fields}, overwrite, cb);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let contract;
|
||||||
|
if(isContractGeneration){
|
||||||
|
contract = {className: contractName, fields: this.fields};
|
||||||
|
try {
|
||||||
|
build(contract, overwrite, cb);
|
||||||
|
} catch(err){
|
||||||
|
this.engine.logger.error(err.message);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Contract exists
|
// Contract already exists
|
||||||
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
||||||
if(_err) throw new Error(_err);
|
if(_err) throw new Error(_err);
|
||||||
const contract = contractsList.find(x => x.className === contractName);
|
const contract = contractsList.find(x => x.className === contractName);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user