From 939642f74cfbfed693a737c7c57c039f0ae5301b Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 12 Oct 2018 14:23:31 -0400 Subject: [PATCH] Validation of datatypes + small refactoring --- cmd/cmd.js | 15 ++++++++- cmd/cmd_controller.js | 3 +- lib/modules/scaffolding-react/index.js | 4 +-- lib/modules/scaffolding-solidity/index.js | 15 ++++----- lib/modules/scaffolding/index.js | 40 +++++++++++++++-------- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 68ba90f0d..19c32b471 100644 --- a/cmd/cmd.js +++ b/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]; }); } diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index fe677ce79..23d1054f6 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -490,8 +490,7 @@ class EmbarkController { }); }, function generateUI(callback){ - engine.events.request("scaffolding:generate", options, () => { - + engine.events.request("scaffolding:generate:ui", options, () => { callback(); }); } diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 167923d27..577a24c40 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -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); diff --git a/lib/modules/scaffolding-solidity/index.js b/lib/modules/scaffolding-solidity/index.js index b72083d63..ce92b9119 100644 --- a/lib/modules/scaffolding-solidity/index.js +++ b/lib/modules/scaffolding-solidity/index.js @@ -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); } diff --git a/lib/modules/scaffolding/index.js b/lib/modules/scaffolding/index.js index 22018cb1b..0544bbd87 100644 --- a/lib/modules/scaffolding/index.js +++ b/lib/modules/scaffolding/index.js @@ -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);