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;
}
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');
process.exit(1);
}
@ -61,18 +61,33 @@ Config.prototype.reloadConfig = function() {
this.loadChainTrackerFile();
};
Config.prototype.loadBlockchainConfigFile = function() {
var defaultBlockchainConfig = {};
if (fs.existsSync(this.configDir + "blockchain.json")) {
defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json");
Config.prototype._mergeConfig = function(configFilename, defaultConfig, env) {
let configFilePath = this.configDir + configFilename;
if (!fs.existsSync(configFilePath)) {
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) {
this.blockchainConfig.enabled = true;
let config = fs.readJSONSync(configFilePath);
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() {
var configObject = {
"default": {
@ -81,9 +96,7 @@ Config.prototype.loadContractsConfigFile = function() {
"solc": "0.4.17"
},
"deployment": {
"host": "localhost",
"port": 8545,
"type": "rpc"
"host": "localhost", "port": 8545, "type": "rpc"
},
"dappConnection": [
"$WEB3",
@ -100,16 +113,7 @@ Config.prototype.loadContractsConfigFile = function() {
configObject = utils.recursiveMerge(configObject, pluginConfig);
});
var contractsConfig;
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;
this.contractsConfig = this._mergeConfig("contracts.json", configObject, this.env);
};
Config.prototype.loadStorageConfigFile = function() {
@ -125,29 +129,10 @@ Config.prototype.loadStorageConfigFile = function() {
"host": "localhost",
"port": 5001,
"getUrl": "http://localhost:8080/ipfs/"
},
"development": {
}
};
var storageConfig;
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 = [];
}
this.storageConfig = this._mergeConfig("storage.json", configObject, this.env);
};
Config.prototype.loadCommunicationConfigFile = function() {
@ -157,48 +142,20 @@ Config.prototype.loadCommunicationConfigFile = function() {
"provider": "whisper",
"available_providers": ["whisper", "orbit"],
"connection": {
"host": "localhost",
"port": 8546,
"type": "ws"
"host": "localhost", "port": 8546, "type": "ws"
}
}
};
var communicationConfig;
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 = [];
}
this.communicationConfig = this._mergeConfig("communication.json", configObject, this.env);
};
Config.prototype.loadWebServerConfigFile = function() {
var webServerConfigJSON;
if (fs.existsSync(this.configDir + "webserver.json")) {
webServerConfigJSON = fs.readJSONSync(this.configDir + "webserver.json");
} else {
webServerConfigJSON = {};
}
var defaultWebConfig = {
"enabled": true,
"host": "localhost",
"port": 8000
var configObject = {
"enabled": true, "host": "localhost", "port": 8000
};
this.webServerConfig = utils.recursiveMerge(defaultWebConfig, webServerConfigJSON);
this.webServerConfig = this._mergeConfig("webserver.json", configObject, false);
};
Config.prototype.loadEmbarkConfigFile = function() {
@ -223,17 +180,12 @@ Config.prototype.loadPipelineConfigFile = function() {
};
Config.prototype.loadChainTrackerFile = function() {
//var self = this;
var chainTracker;
try {
chainTracker = fs.readJSONSync(this.chainsFile);
}
catch(err) {
//self.logger.info(this.chainsFile + ' file not found, creating it...');
chainTracker = {};
if (!fs.existsSync(this.chainsFile)) {
this.logger.info(this.chainsFile + ' file not found, creating it...');
fs.writeJSONSync(this.chainsFile, {});
}
this.chainTracker = chainTracker;
this.chainTracker = fs.readJSONSync(this.chainsFile);
};
Config.prototype.loadFiles = function(files) {
@ -249,7 +201,6 @@ Config.prototype.loadFiles = function(files) {
var filesFromPlugins = [];
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
if (filePlugins.length > 0) {
filePlugins.forEach(function(plugin) {
try {
var fileObjects = plugin.runFilePipeline();
@ -262,7 +213,6 @@ Config.prototype.loadFiles = function(files) {
self.logger.error(err.message);
}
});
}
filesFromPlugins.filter(function(file) {
if (utils.fileMatchesPattern(files, file.intendedPath)) {
readFiles.push(file);
@ -276,17 +226,14 @@ Config.prototype.loadPluginContractFiles = function() {
var self = this;
var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
if (contractsPlugins.length > 0) {
contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) {
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) {
callback(plugin.loadPluginFile(file));
}}));
});
});
}
};
module.exports = Config;