Error handling, added overwrite, fixed duplication of components
This commit is contained in:
parent
fe51abc2bb
commit
b93d2d1145
|
@ -328,6 +328,8 @@ class Cmd {
|
||||||
program
|
program
|
||||||
.command('scaffold [contract] [environment]')
|
.command('scaffold [contract] [environment]')
|
||||||
.option('--framework <framework>', 'UI framework to use. (default: react)')
|
.option('--framework <framework>', 'UI framework to use. (default: react)')
|
||||||
|
.option('--overwrite', 'Overwrite existing files. (default: false)')
|
||||||
|
|
||||||
.action(function(contract, env, options){
|
.action(function(contract, env, options){
|
||||||
if(contract === undefined){
|
if(contract === undefined){
|
||||||
console.log("contract name is required");
|
console.log("contract name is required");
|
||||||
|
@ -344,6 +346,8 @@ class Cmd {
|
||||||
options.webpackConfigName = options.pipeline || 'development';
|
options.webpackConfigName = options.pipeline || 'development';
|
||||||
options.contract = contract;
|
options.contract = contract;
|
||||||
options.framework = options.framework || 'react';
|
options.framework = options.framework || 'react';
|
||||||
|
options.overwrite = options.overwrite || false;
|
||||||
|
|
||||||
embark.scaffold(options);
|
embark.scaffold(options);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -494,9 +494,7 @@ class EmbarkController {
|
||||||
|
|
||||||
|
|
||||||
let scaffold = new Scaffolding(engine, options);
|
let scaffold = new Scaffolding(engine, options);
|
||||||
scaffold.generate(options.contract);
|
scaffold.generate(options.contract, options.overwrite);
|
||||||
|
|
||||||
engine.logger.info(__("finished generating the UI").underline);
|
|
||||||
callback(err, true);
|
callback(err, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ class Scaffolding {
|
||||||
return this.engine.config.contractsConfig.contracts[contractName] !== undefined;
|
return this.engine.config.contractsConfig.contracts[contractName] !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
generate(contractName){
|
generate(contractName, overwrite){
|
||||||
if(this.framework === 'react'){
|
if(this.framework === 'react'){
|
||||||
this.engine.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
this.engine.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,12 @@ class Scaffolding {
|
||||||
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);
|
||||||
build(contract);
|
try {
|
||||||
|
build(contract, overwrite);
|
||||||
|
this.engine.logger.info(__("finished generating the UI").underline);
|
||||||
|
} catch(err){
|
||||||
|
this.engine.logger.error(err.message);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,10 +48,10 @@ class ScaffoldingReact {
|
||||||
this.embark.registerDappGenerator('react', this.build.bind(this));
|
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 filename = contract.className.toLowerCase() + '.' + extension;
|
||||||
const filePath = './app/' + filename;
|
const filePath = './app/' + filename;
|
||||||
if (fs.existsSync(filePath)){
|
if (!overwrite && fs.existsSync(filePath)){
|
||||||
throw new Error("file '" + filePath + "' already exists");
|
throw new Error("file '" + filePath + "' already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,26 +64,22 @@ class ScaffoldingReact {
|
||||||
fs.writeFileSync(filePath, result);
|
fs.writeFileSync(filePath, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
_buildHTML(contract){
|
build(contract, overwrite){
|
||||||
const filename = contract.className.toLowerCase();
|
const filename = contract.className.toLowerCase();
|
||||||
|
|
||||||
this._generateFile(contract, 'index.html.tpl', 'html',
|
this._generateFile(contract, 'index.html.tpl', 'html',
|
||||||
{
|
{
|
||||||
'title': contract.className,
|
'title': contract.className,
|
||||||
'filename': filename
|
filename
|
||||||
});
|
}, overwrite);
|
||||||
}
|
|
||||||
|
|
||||||
async build(contract){
|
|
||||||
this._buildHTML(contract);
|
|
||||||
|
|
||||||
const filename = contract.className.toLowerCase();
|
|
||||||
|
|
||||||
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
||||||
{
|
{
|
||||||
'title': contract.className,
|
'title': contract.className,
|
||||||
'contractName': contract.className,
|
'contractName': contract.className,
|
||||||
'functions': contract.abiDefinition.filter(x => x.type === 'function')
|
'functions': contract.abiDefinition.filter(x => x.type === 'function')
|
||||||
});
|
}, overwrite);
|
||||||
|
|
||||||
|
|
||||||
// Update config
|
// Update config
|
||||||
const contents = fs.readFileSync("./embark.json");
|
const contents = fs.readFileSync("./embark.json");
|
||||||
|
@ -94,6 +90,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(filename + ".html generated");
|
||||||
|
this.embark.logger.info(filename + ".js generated");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -179,23 +179,6 @@ class {{contractName}}UI extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class {{contractName}}UI extends React.Component {
|
|
||||||
constructor (props) {
|
|
||||||
super(props);
|
|
||||||
this.state = {
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
render(){
|
|
||||||
return (<div>
|
|
||||||
{{#each functions}}
|
|
||||||
<{{capitalize name}}_{{@index}}_Form />
|
|
||||||
{{/each}}
|
|
||||||
</div>);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ReactDOM.render(<div>
|
ReactDOM.render(<div>
|
||||||
<h1>{{title}}</h1>
|
<h1>{{title}}</h1>
|
||||||
<{{contractName}}UI />
|
<{{contractName}}UI />
|
||||||
|
|
Loading…
Reference in New Issue