diff --git a/cmd/cmd.js b/cmd/cmd.js index 8ae88234..1c253686 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -328,12 +328,14 @@ class Cmd { program .command('scaffold [contract] [environment]') .option('--framework ', 'UI framework to use. (default: react)') + .option('--overwrite', 'Overwrite existing files. (default: false)') + .action(function(contract, env, options){ if(contract === undefined){ console.log("contract name is required"); process.exit(0); } - + checkDeps(); i18n.setOrDetectLocale(options.locale); options.env = env || 'development'; @@ -344,6 +346,8 @@ class Cmd { options.webpackConfigName = options.pipeline || 'development'; options.contract = contract; options.framework = options.framework || 'react'; + options.overwrite = options.overwrite || false; + embark.scaffold(options); }); } diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 7ddc3284..1f7a789c 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -494,9 +494,7 @@ class EmbarkController { let scaffold = new Scaffolding(engine, options); - scaffold.generate(options.contract); - - engine.logger.info(__("finished generating the UI").underline); + scaffold.generate(options.contract, options.overwrite); callback(err, true); }); } diff --git a/lib/cmds/scaffolding.js b/lib/cmds/scaffolding.js index 2bfe30c9..d92e34a1 100644 --- a/lib/cmds/scaffolding.js +++ b/lib/cmds/scaffolding.js @@ -10,7 +10,7 @@ class Scaffolding { return this.engine.config.contractsConfig.contracts[contractName] !== undefined; } - generate(contractName){ + generate(contractName, overwrite){ if(this.framework === 'react'){ this.engine.plugins.loadInternalPlugin('scaffolding-react', this.options); } @@ -38,7 +38,12 @@ class Scaffolding { if(_err) throw new Error(_err); const contract = contractsList.find(x => x.className === contractName); - build(contract); + try { + build(contract, overwrite); + this.engine.logger.info(__("finished generating the UI").underline); + } catch(err){ + this.engine.logger.error(err.message); + } }); } } diff --git a/lib/modules/scaffolding-react/index.js b/lib/modules/scaffolding-react/index.js index 29e14750..c11d744e 100644 --- a/lib/modules/scaffolding-react/index.js +++ b/lib/modules/scaffolding-react/index.js @@ -48,10 +48,10 @@ class ScaffoldingReact { this.embark.registerDappGenerator('react', this.build.bind(this)); } - _generateFile(contract, templateFilename, extension, data){ + _generateFile(contract, templateFilename, extension, data, overwrite){ const filename = contract.className.toLowerCase() + '.' + extension; const filePath = './app/' + filename; - if (fs.existsSync(filePath)){ + if (!overwrite && fs.existsSync(filePath)){ throw new Error("file '" + filePath + "' already exists"); } @@ -64,26 +64,22 @@ class ScaffoldingReact { fs.writeFileSync(filePath, result); } - _buildHTML(contract){ + build(contract, overwrite){ const filename = contract.className.toLowerCase(); + this._generateFile(contract, 'index.html.tpl', 'html', { 'title': contract.className, - 'filename': filename - }); - } - - async build(contract){ - this._buildHTML(contract); - - const filename = contract.className.toLowerCase(); + filename + }, overwrite); this._generateFile(contract, 'dapp.js.tpl', 'js', { 'title': contract.className, 'contractName': contract.className, 'functions': contract.abiDefinition.filter(x => x.type === 'function') - }); + }, overwrite); + // Update config const contents = fs.readFileSync("./embark.json"); @@ -94,6 +90,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"); + } } diff --git a/lib/modules/scaffolding-react/templates/dapp.js.tpl b/lib/modules/scaffolding-react/templates/dapp.js.tpl index 023c64a0..c0827fc7 100644 --- a/lib/modules/scaffolding-react/templates/dapp.js.tpl +++ b/lib/modules/scaffolding-react/templates/dapp.js.tpl @@ -179,23 +179,6 @@ class {{contractName}}UI extends React.Component { } -class {{contractName}}UI extends React.Component { - constructor (props) { - super(props); - this.state = { - }; - } - - render(){ - return (
- {{#each functions}} - <{{capitalize name}}_{{@index}}_Form /> - {{/each}} -
); - } -} - - ReactDOM.render(

{{title}}

<{{contractName}}UI />