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 6d0f4b67ae
commit 0eaafb77f1
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
5 changed files with 139 additions and 0 deletions

View File

@ -46,6 +46,7 @@ class Cmd {
this.reset(); this.reset();
this.ejectWebpack(); this.ejectWebpack();
this.graph(); this.graph();
this.scaffold();
this.upload(); this.upload();
this.versionCmd(); this.versionCmd();
this.helpCmd(); this.helpCmd();
@ -323,6 +324,29 @@ class Cmd {
}); });
} }
scaffold() {
program
.command('scaffold [contract] [environment]')
.option('--framework', 'UI framework to use. (default: react)')
.action(function(contract, env, options){
let environment = env || 'development';
if(contract === undefined){
console.log("contract name is required");
process.exit(0);
}
embark.initConfig(environment, {
embarkConfig: 'embark.json', interceptLogs: false
});
options.contract = contract;
options.framework = options.framework || 'react';
options.env = environment;
embark.scaffold(options);
});
}
reset() { reset() {
program program
.command('reset') .command('reset')

View File

@ -123,6 +123,10 @@ Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
this.addPluginType('pipeline'); this.addPluginType('pipeline');
}; };
Plugin.prototype.registerCustomType = function(type){
this.pluginTypes.push(type);
}
Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) { Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) {
this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options}); this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options});
this.addPluginType('pipelineFiles'); this.addPluginType('pipelineFiles');

View File

@ -358,6 +358,42 @@ class Embark {
resetCmd(); resetCmd();
} }
scaffold(options) {
// initialise embark engine
let engine = new Engine({
env: options.env,
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logfile: options.logfile
});
engine.init();
// load plugins
this.plugins.loadInternalPlugin('scaffolding', options);
let scaffoldPlugin = this.plugins.plugins.filter(x => x.name == "scaffolding")[0].pluginModule;
let self = this;
async.waterfall([
function generateUI(callback){
engine.events.on('outputDone', function () {
let scaffold = new scaffoldPlugin(engine, options);
let result = scaffold.generate(options.contract);
engine.logger.info(result);
callback();
});
self.build(options, engine, true);
}
], function (err, _result) {
if (err) {
engine.logger.error(err.message);
engine.logger.debug(err.stack);
} else {
engine.logger.info("Finished generating ui");
}
process.exit();
});
}
upload(options) { upload(options) {
this.context = options.context || [constants.contexts.upload, constants.contexts.build]; this.context = options.context || [constants.contexts.upload, constants.contexts.build];

View File

@ -0,0 +1,14 @@
class ScaffoldingReact {
constructor(embark, options){
this.embark = embark;
this.options = options;
}
build(contract){
}
}
module.exports = ScaffoldingReact;

View File

@ -0,0 +1,61 @@
const fs = require('fs');
const commandName = "generate-ui";
const formatReplyMsg = (message) => commandName + ": " + message;
class Scaffolding {
constructor(embark, options){
this.embark = embark;
this.options = options;
this.framework = options.framework;
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){
return this.embark.config.contractsConfig.contracts[contractName] !== undefined;
}
generate(contractName){
let frameworkPlugin;
if(this.framework == 'react'){
this.embark.plugins.loadInternalPlugin('scaffolding-react', this.options);
frameworkPlugin = this.embark.plugins.plugins.filter(x => x.name == "scaffolding-react")[0].pluginModule;
} else {
let plugins = this.embark.plugins.getPluginsFor(this.framework);
if(plugins.length !== 1){
return formatReplyMsg("Could not find plugin for framework '" + this.framework + "'");
}
frameworkPlugin = plugins[0].pluginModule;
}
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);
uiFramework.build(contract);
} catch(err){
return err;
}
return formatReplyMsg("done!");
}
}
module.exports = Scaffolding;