refactor config code

This commit is contained in:
Iuri Matias 2018-01-19 22:08:39 -05:00
parent 10c4c75236
commit b19194fc6b
1 changed files with 54 additions and 107 deletions

View File

@ -25,7 +25,7 @@ Config.prototype.loadConfigFiles = function(options) {
interceptLogs = true; interceptLogs = true;
} }
if(!fs.existsSync(options.embarkConfig)){ if (!fs.existsSync(options.embarkConfig)){
this.logger.error('Cannot find file ' + options.embarkConfig + '. Please ensure you are running this command inside the Dapp folder'); this.logger.error('Cannot find file ' + options.embarkConfig + '. Please ensure you are running this command inside the Dapp folder');
process.exit(1); process.exit(1);
} }
@ -61,18 +61,33 @@ Config.prototype.reloadConfig = function() {
this.loadChainTrackerFile(); this.loadChainTrackerFile();
}; };
Config.prototype.loadBlockchainConfigFile = function() { Config.prototype._mergeConfig = function(configFilename, defaultConfig, env) {
var defaultBlockchainConfig = {}; let configFilePath = this.configDir + configFilename;
if (fs.existsSync(this.configDir + "blockchain.json")) { if (!fs.existsSync(configFilePath)) {
defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json"); this.logger.warn("no config file found at " + configFilePath + ". using default config");
return defaultConfig['default'] || {};
} }
this.blockchainConfig = defaultBlockchainConfig[this.env] || {};
if (this.blockchainConfig.enabled === undefined) { let config = fs.readJSONSync(configFilePath);
this.blockchainConfig.enabled = true; let configObject = utils.recursiveMerge(defaultConfig, config);
if (env) {
return utils.recursiveMerge(configObject['default'] || {}, configObject[env]);
} else {
return utils.recursiveMerge(configObject || {}, configObject || {});
} }
}; };
Config.prototype.loadBlockchainConfigFile = function() {
var configObject = {
"default": {
"enabled": true
}
};
this.blockchainConfig = this._mergeConfig("blockchain.json", configObject, this.env);
};
Config.prototype.loadContractsConfigFile = function() { Config.prototype.loadContractsConfigFile = function() {
var configObject = { var configObject = {
"default": { "default": {
@ -81,9 +96,7 @@ Config.prototype.loadContractsConfigFile = function() {
"solc": "0.4.17" "solc": "0.4.17"
}, },
"deployment": { "deployment": {
"host": "localhost", "host": "localhost", "port": 8545, "type": "rpc"
"port": 8545,
"type": "rpc"
}, },
"dappConnection": [ "dappConnection": [
"$WEB3", "$WEB3",
@ -100,16 +113,7 @@ Config.prototype.loadContractsConfigFile = function() {
configObject = utils.recursiveMerge(configObject, pluginConfig); configObject = utils.recursiveMerge(configObject, pluginConfig);
}); });
var contractsConfig; this.contractsConfig = this._mergeConfig("contracts.json", configObject, this.env);
if (fs.existsSync(this.configDir + "contracts.json")) {
contractsConfig = fs.readJSONSync(this.configDir + "contracts.json");
configObject = utils.recursiveMerge(configObject, contractsConfig);
}
var defaultContractsConfig = configObject['default'];
var envContractsConfig = configObject[this.env];
var mergedConfig = utils.recursiveMerge(defaultContractsConfig, envContractsConfig);
this.contractsConfig = mergedConfig;
}; };
Config.prototype.loadStorageConfigFile = function() { Config.prototype.loadStorageConfigFile = function() {
@ -125,29 +129,10 @@ Config.prototype.loadStorageConfigFile = function() {
"host": "localhost", "host": "localhost",
"port": 5001, "port": 5001,
"getUrl": "http://localhost:8080/ipfs/" "getUrl": "http://localhost:8080/ipfs/"
},
"development": {
} }
}; };
var storageConfig; this.storageConfig = this._mergeConfig("storage.json", configObject, this.env);
if (fs.existsSync(this.configDir + "storage.json")) {
storageConfig = fs.readJSONSync(this.configDir + "storage.json");
configObject = utils.recursiveMerge(configObject, storageConfig);
}
var defaultStorageConfig = configObject['default'];
var envStorageConfig = configObject[this.env];
var mergedConfig = utils.recursiveMerge(defaultStorageConfig, envStorageConfig);
this.storageConfig = mergedConfig || {};
if (this.storageConfig.enabled === undefined) {
this.storageConfig.enabled = true;
}
if (this.storageConfig.available_providers === undefined) {
this.storageConfig.available_providers = [];
}
}; };
Config.prototype.loadCommunicationConfigFile = function() { Config.prototype.loadCommunicationConfigFile = function() {
@ -157,48 +142,20 @@ Config.prototype.loadCommunicationConfigFile = function() {
"provider": "whisper", "provider": "whisper",
"available_providers": ["whisper", "orbit"], "available_providers": ["whisper", "orbit"],
"connection": { "connection": {
"host": "localhost", "host": "localhost", "port": 8546, "type": "ws"
"port": 8546,
"type": "ws"
} }
} }
}; };
var communicationConfig; this.communicationConfig = this._mergeConfig("communication.json", configObject, this.env);
if (fs.existsSync(this.configDir + "communication.json")) {
communicationConfig = fs.readJSONSync(this.configDir + "communication.json");
configObject = utils.recursiveMerge(configObject, communicationConfig);
}
var defaultCommunicationConfig = configObject['default'];
var envCommunicationConfig = configObject[this.env];
var mergedConfig = utils.recursiveMerge(defaultCommunicationConfig, envCommunicationConfig);
this.communicationConfig = mergedConfig || {};
// TODO: probably not necessary if the default object is done right
if (this.communicationConfig.enabled === undefined) {
this.communicationConfig.enabled = true;
}
if (this.communicationConfig.available_providers === undefined) {
this.communicationConfig.available_providers = [];
}
}; };
Config.prototype.loadWebServerConfigFile = function() { Config.prototype.loadWebServerConfigFile = function() {
var webServerConfigJSON; var configObject = {
if (fs.existsSync(this.configDir + "webserver.json")) { "enabled": true, "host": "localhost", "port": 8000
webServerConfigJSON = fs.readJSONSync(this.configDir + "webserver.json");
} else {
webServerConfigJSON = {};
}
var defaultWebConfig = {
"enabled": true,
"host": "localhost",
"port": 8000
}; };
this.webServerConfig = utils.recursiveMerge(defaultWebConfig, webServerConfigJSON);
this.webServerConfig = this._mergeConfig("webserver.json", configObject, false);
}; };
Config.prototype.loadEmbarkConfigFile = function() { Config.prototype.loadEmbarkConfigFile = function() {
@ -223,17 +180,12 @@ Config.prototype.loadPipelineConfigFile = function() {
}; };
Config.prototype.loadChainTrackerFile = function() { Config.prototype.loadChainTrackerFile = function() {
//var self = this; if (!fs.existsSync(this.chainsFile)) {
var chainTracker; this.logger.info(this.chainsFile + ' file not found, creating it...');
try {
chainTracker = fs.readJSONSync(this.chainsFile);
}
catch(err) {
//self.logger.info(this.chainsFile + ' file not found, creating it...');
chainTracker = {};
fs.writeJSONSync(this.chainsFile, {}); fs.writeJSONSync(this.chainsFile, {});
} }
this.chainTracker = chainTracker;
this.chainTracker = fs.readJSONSync(this.chainsFile);
}; };
Config.prototype.loadFiles = function(files) { Config.prototype.loadFiles = function(files) {
@ -249,7 +201,6 @@ Config.prototype.loadFiles = function(files) {
var filesFromPlugins = []; var filesFromPlugins = [];
var filePlugins = self.plugins.getPluginsFor('pipelineFiles'); var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
if (filePlugins.length > 0) {
filePlugins.forEach(function(plugin) { filePlugins.forEach(function(plugin) {
try { try {
var fileObjects = plugin.runFilePipeline(); var fileObjects = plugin.runFilePipeline();
@ -262,7 +213,6 @@ Config.prototype.loadFiles = function(files) {
self.logger.error(err.message); self.logger.error(err.message);
} }
}); });
}
filesFromPlugins.filter(function(file) { filesFromPlugins.filter(function(file) {
if (utils.fileMatchesPattern(files, file.intendedPath)) { if (utils.fileMatchesPattern(files, file.intendedPath)) {
readFiles.push(file); readFiles.push(file);
@ -276,17 +226,14 @@ Config.prototype.loadPluginContractFiles = function() {
var self = this; var self = this;
var contractsPlugins = this.plugins.getPluginsFor('contractFiles'); var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
if (contractsPlugins.length > 0) {
contractsPlugins.forEach(function(plugin) { contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) { plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./',''); var filename = file.replace('./','');
//self.contractsFiles.push({filename: filename, content: plugin.loadPluginFile(file), path: plugin.pathToFile(file)});
self.contractsFiles.push(new File({filename: filename, type: 'custom', resolver: function(callback) { self.contractsFiles.push(new File({filename: filename, type: 'custom', resolver: function(callback) {
callback(plugin.loadPluginFile(file)); callback(plugin.loadPluginFile(file));
}})); }}));
}); });
}); });
}
}; };
module.exports = Config; module.exports = Config;