Adding base command logic for scaffolding generation

This commit is contained in:
Richard Ramos 2018-05-09 17:02:17 -04:00 committed by Pascal Precht
parent ea16a865bd
commit 2aa00a415b
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D

View File

@ -2,7 +2,8 @@ const fs = require('fs');
const commandName = "generate-ui"; const commandName = "generate-ui";
const errorMessage = (message) => new Error(commandName + ": " + message); //const errorMessage = (message) => new Error(commandName + ": " + message);
const formatReplyMsg = (message) => commandName + ": " + message;
class Scaffolding { class Scaffolding {
constructor(embark, options){ constructor(embark, options){
@ -12,6 +13,15 @@ class Scaffolding {
this.frameworkPlugin = null; this.frameworkPlugin = null;
} }
createDirectories(contractName){
const dir = './app/' + contractName;
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
} else {
throw formatReplyMsg("directory ./app/" + contractName + " already exists");
}
}
isContract(contractName){ isContract(contractName){
return this.embark.config.contractsConfig.contracts[contractName] !== undefined; return this.embark.config.contractsConfig.contracts[contractName] !== undefined;
} }
@ -25,25 +35,27 @@ class Scaffolding {
} else { } else {
let plugins = this.embark.plugins.getPluginsFor(this.framework); let plugins = this.embark.plugins.getPluginsFor(this.framework);
if(plugins.length !== 1){ if(plugins.length !== 1){
throw errorMessage("Could not find plugin for framework '" + this.framework + "'"); //throw errorMessage("Could not find plugin for framework '" + this.framework + "'");
return formatReplyMsg("Could not find plugin for framework '" + this.framework + "'");
} }
frameworkPlugin = plugins[0].pluginModule; frameworkPlugin = plugins[0].pluginModule;
} }
if(!this.isContract(contractName)){
return errorMessage("contract '" + contractName + "' does not exist");
}
const contract = this.embark.config.contractsConfig.contracts[contractName];
try { try {
if(!this.isContract(contractName)){
return formatReplyMsg("contract '" + contractName + "' does not exist");
}
const contract = this.embark.config.contractsConfig.contracts[contractName];
this.createDirectories(contractName);
let uiFramework = new frameworkPlugin(this.embark, this.options); let uiFramework = new frameworkPlugin(this.embark, this.options);
let result = uiFramework.build(contract); uiFramework.build(contract);
this.embark.logger.info(result);
} catch(err){ } catch(err){
throw errorMessage(err); return err;
} }
return formatReplyMsg("done!");
} }
} }