reduce complexity of engine main metod

This commit is contained in:
Iuri Matias 2017-03-03 21:48:32 -05:00
parent 383d69c194
commit 617d263341
1 changed files with 105 additions and 76 deletions

View File

@ -27,87 +27,116 @@ Engine.prototype.init = function(_options) {
};
Engine.prototype.startService = function(serviceName, _options) {
var self = this;
var options = _options || {};
if (serviceName === "monitor") {
var servicesMonitor = new ServicesMonitor({
logger: this.logger,
config: this.config,
serverHost: options.serverHost,
serverPort: options.serverPort,
runWebserver: options.runWebserver,
version: this.version
});
servicesMonitor.startMonitor();
} else if (serviceName === "pipeline") {
this.logger.setStatus("Building Assets");
var pipeline = new Pipeline({
buildDir: this.config.buildDir,
contractsFiles: this.config.contractsFiles,
assetFiles: this.config.assetFiles,
logger: this.logger,
plugins: this.plugins
});
this.events.on('abi', function(abi) {
self.currentAbi = abi;
pipeline.build(abi);
});
this.events.on('file-event', function(fileType, path) {
if (fileType === 'asset') {
self.config.reloadConfig();
pipeline.build(self.abi, path);
}
});
} else if (serviceName === "abi") {
var generateABICode = function(contractsManager) {
var abiGenerator = new ABIGenerator({
blockchainConfig: self.config.blockchainConfig,
contractsManager: contractsManager,
plugins: self.plugins,
storageConfig: self.config.storageConfig,
communicationConfig: self.config.communicationConfig
});
var embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
var vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
var vanillaContractsABI = abiGenerator.generateContracts(false);
self.events.emit('abi-contracts-vanila', vanillaContractsABI);
self.events.emit('abi-vanila', vanillaABI);
self.events.emit('abi', embarkJSABI);
};
this.events.on('contractsDeployed', generateABICode);
this.events.on('blockchainDisabled', generateABICode);
} else if (serviceName === "deployment") {
this.deployManager = new DeployManager({
web3: options.web3,
trackContracts: options.trackContracts,
config: this.config,
logger: this.logger,
plugins: this.plugins,
events: this.events
});
var services = {
"monitor": this.monitorService,
"pipeline": this.pipelineService,
"abi": this.abiService,
"deployment": this.deploymentService,
"fileWatcher": this.fileWatchService,
"webServer": this.webServerService
};
this.events.on('file-event', function(fileType, path) {
if (fileType === 'contract' || fileType === 'config') {
self.config.reloadConfig();
deployManager.deployContracts(function() {});
}
});
} else if (serviceName === "fileWatcher") {
this.logger.setStatus("Watching for changes");
var watch = new Watch({logger: this.logger, events: this.events});
watch.start();
} else if (serviceName === "webServer") {
this.logger.setStatus("Starting Server");
var server = new Server({
logger: this.logger,
host: options.serverHost,
port: options.serverPort
});
server.start(function(){});
} else {
var service = services[serviceName];
if (!service) {
throw new Error("unknown service: " + serviceName);
}
this.logger.trace("calling: " + serviceName + "(" + JSON.stringify(options) + ")");
return service.apply(this, [options]);
};
Engine.prototype.monitorService = function(options) {
var servicesMonitor = new ServicesMonitor({
logger: this.logger,
config: this.config,
serverHost: options.serverHost,
serverPort: options.serverPort,
runWebserver: options.runWebserver,
version: this.version
});
servicesMonitor.startMonitor();
};
Engine.prototype.pipelineService = function(options) {
var self = this;
this.logger.setStatus("Building Assets");
var pipeline = new Pipeline({
buildDir: this.config.buildDir,
contractsFiles: this.config.contractsFiles,
assetFiles: this.config.assetFiles,
logger: this.logger,
plugins: this.plugins
});
this.events.on('abi', function(abi) {
self.currentAbi = abi;
pipeline.build(abi);
});
this.events.on('file-event', function(fileType, path) {
if (fileType === 'asset') {
self.config.reloadConfig();
pipeline.build(self.abi, path);
}
});
};
Engine.prototype.abiService = function(options) {
var self = this;
var generateABICode = function(contractsManager) {
var abiGenerator = new ABIGenerator({
blockchainConfig: self.config.blockchainConfig,
contractsManager: contractsManager,
plugins: self.plugins,
storageConfig: self.config.storageConfig,
communicationConfig: self.config.communicationConfig
});
var embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
var vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
var vanillaContractsABI = abiGenerator.generateContracts(false);
self.events.emit('abi-contracts-vanila', vanillaContractsABI);
self.events.emit('abi-vanila', vanillaABI);
self.events.emit('abi', embarkJSABI);
};
this.events.on('contractsDeployed', generateABICode);
this.events.on('blockchainDisabled', generateABICode);
};
Engine.prototype.deploymentService = function(options) {
var self = this;
this.deployManager = new DeployManager({
web3: options.web3,
trackContracts: options.trackContracts,
config: this.config,
logger: this.logger,
plugins: this.plugins,
events: this.events
});
this.events.on('file-event', function(fileType, path) {
if (fileType === 'contract' || fileType === 'config') {
self.config.reloadConfig();
self.deployManager.deployContracts(function() {});
}
});
};
Engine.prototype.fileWatchService = function(options) {
this.logger.setStatus("Watching for changes");
var watch = new Watch({logger: this.logger, events: this.events});
watch.start();
};
Engine.prototype.webServerService = function(options) {
this.logger.setStatus("Starting Server");
var server = new Server({
logger: this.logger,
host: options.serverHost,
port: options.serverPort
});
server.start(function(){});
};
module.exports = Engine;