From 617d263341de625095a6467ee3652cb93b215354 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 3 Mar 2017 21:48:32 -0500 Subject: [PATCH] reduce complexity of engine main metod --- lib/core/engine.js | 181 ++++++++++++++++++++++++++------------------- 1 file changed, 105 insertions(+), 76 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 6e4bc01f..527b2c8e 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -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;