embark-area-51/lib/core/config.js

240 lines
6.8 KiB
JavaScript
Raw Normal View History

2017-03-31 11:39:33 +00:00
var fs = require('./fs.js');
var File = require('./file.js');
2017-03-31 11:39:33 +00:00
var Plugins = require('./plugins.js');
var utils = require('../utils/utils.js');
2016-08-22 03:40:05 +00:00
2017-03-31 11:39:33 +00:00
var Config = function(options) {
this.env = options.env;
this.blockchainConfig = {};
this.contractsConfig = {};
this.pipelineConfig = {};
this.webServerConfig = {};
this.chainTracker = {};
this.assetFiles = {};
this.contractsFiles = [];
this.configDir = options.configDir || 'config/';
this.chainsFile = options.chainsFile || './chains.json';
this.plugins = options.plugins;
this.logger = options.logger;
this.events = options.events;
};
2016-08-22 03:40:05 +00:00
2017-03-31 11:39:33 +00:00
Config.prototype.loadConfigFiles = function(options) {
var interceptLogs = options.interceptLogs;
2017-02-06 11:42:58 +00:00
if (options.interceptLogs === undefined) {
interceptLogs = true;
}
2018-01-20 03:08:39 +00:00
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);
}
2017-02-19 03:40:42 +00:00
this.embarkConfig = fs.readJSONSync(options.embarkConfig);
2017-01-29 06:28:01 +00:00
this.embarkConfig.plugins = this.embarkConfig.plugins || {};
2016-08-22 03:40:05 +00:00
2017-03-31 11:39:33 +00:00
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this});
2017-01-26 11:34:00 +00:00
this.plugins.loadPlugins();
2017-03-31 11:39:33 +00:00
this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadContractsConfigFile();
2017-03-31 11:39:33 +00:00
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
2017-03-29 17:04:35 +00:00
this.loadWebServerConfigFile();
this.loadChainTrackerFile();
this.loadPluginContractFiles();
2016-08-22 03:40:05 +00:00
};
2017-03-31 11:39:33 +00:00
Config.prototype.reloadConfig = function() {
this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadContractsConfigFile();
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
2016-09-25 01:10:47 +00:00
this.loadChainTrackerFile();
};
2016-08-22 03:40:05 +00:00
2018-01-20 03:08:39 +00:00
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'] || {};
}
2018-01-20 03:08:39 +00:00
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 || {});
}
2016-08-22 03:40:05 +00:00
};
2018-01-20 03:08:39 +00:00
Config.prototype.loadBlockchainConfigFile = function() {
var configObject = {
"default": {
"enabled": true
}
};
this.blockchainConfig = this._mergeConfig("blockchain.json", configObject, this.env);
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadContractsConfigFile = function() {
var configObject = {
"default": {
"versions": {
"web3.js": "1.0.0-beta",
"solc": "0.4.17"
},
"deployment": {
2018-01-20 03:08:39 +00:00
"host": "localhost", "port": 8545, "type": "rpc"
},
"dappConnection": [
"$WEB3",
"localhost:8545"
],
"gas": "auto",
"contracts": {
}
}
};
2017-03-31 11:39:33 +00:00
var contractsConfigs = this.plugins.getPluginsProperty('contractsConfig', 'contractsConfigs');
contractsConfigs.forEach(function(pluginConfig) {
configObject = utils.recursiveMerge(configObject, pluginConfig);
});
2017-01-26 11:34:00 +00:00
2018-01-20 03:08:39 +00:00
this.contractsConfig = this._mergeConfig("contracts.json", configObject, this.env);
2016-08-22 03:40:05 +00:00
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadStorageConfigFile = function() {
var configObject = {
"default": {
2018-01-10 16:15:32 +00:00
"versions": {
"ipfs-api": "17.2.4"
},
"enabled": true,
"available_providers": ["ipfs"],
"ipfs_bin": "ipfs",
"provider": "ipfs",
"host": "localhost",
2017-07-23 12:15:40 +00:00
"port": 5001,
"getUrl": "http://localhost:8080/ipfs/"
2017-03-31 11:39:33 +00:00
}
};
2018-01-20 03:08:39 +00:00
this.storageConfig = this._mergeConfig("storage.json", configObject, this.env);
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadCommunicationConfigFile = function() {
var configObject = {
"default": {
"enabled": true,
"provider": "whisper",
"available_providers": ["whisper", "orbit"],
"connection": {
2018-01-20 03:08:39 +00:00
"host": "localhost", "port": 8546, "type": "ws"
}
}
};
2018-01-20 03:08:39 +00:00
this.communicationConfig = this._mergeConfig("communication.json", configObject, this.env);
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadWebServerConfigFile = function() {
2018-01-20 03:08:39 +00:00
var configObject = {
"enabled": true, "host": "localhost", "port": 8000
};
2018-01-20 03:08:39 +00:00
this.webServerConfig = this._mergeConfig("webserver.json", configObject, false);
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadEmbarkConfigFile = function() {
var contracts = this.embarkConfig.contracts;
2016-08-22 03:40:05 +00:00
this.contractsFiles = this.loadFiles(contracts);
2017-12-15 22:14:00 +00:00
// determine contract 'root' directories
this.contractDirectories = contracts.map((dir) => {
return dir.split("**")[0];
}).map((dir) => {
return dir.split("*.")[0];
});
2016-08-22 03:40:05 +00:00
2017-03-31 11:39:33 +00:00
this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config;
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadPipelineConfigFile = function() {
var assets = this.embarkConfig.app;
for(var targetFile in assets) {
2017-12-12 17:20:57 +00:00
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
2016-08-22 03:40:05 +00:00
}
};
2016-09-25 01:10:47 +00:00
2017-03-31 11:39:33 +00:00
Config.prototype.loadChainTrackerFile = function() {
2018-01-20 03:08:39 +00:00
if (!fs.existsSync(this.chainsFile)) {
this.logger.info(this.chainsFile + ' file not found, creating it...');
2017-02-19 03:40:42 +00:00
fs.writeJSONSync(this.chainsFile, {});
2016-09-25 01:10:47 +00:00
}
2018-01-20 03:08:39 +00:00
this.chainTracker = fs.readJSONSync(this.chainsFile);
2016-09-25 01:10:47 +00:00
};
2016-08-22 03:40:05 +00:00
2017-03-31 11:39:33 +00:00
Config.prototype.loadFiles = function(files) {
var self = this;
var originalFiles = utils.filesMatchingPattern(files);
var readFiles = [];
2016-08-22 03:40:05 +00:00
2017-03-31 11:39:33 +00:00
originalFiles.filter(function(file) {
2017-07-03 22:54:31 +00:00
return (file[0] === '$' || file.indexOf('.') >= 0);
2017-03-31 11:39:33 +00:00
}).filter(function(file) {
2017-12-12 17:20:57 +00:00
readFiles.push(new File({filename: file, type: "dapp_file", path: file}));
2016-08-22 03:40:05 +00:00
});
2017-03-31 11:39:33 +00:00
var filesFromPlugins = [];
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
2018-01-20 03:08:39 +00:00
filePlugins.forEach(function(plugin) {
try {
var fileObjects = plugin.runFilePipeline();
for (var i=0; i < fileObjects.length; i++) {
var fileObject = fileObjects[i];
filesFromPlugins.push(fileObject);
2017-02-03 11:30:08 +00:00
}
2018-01-20 03:08:39 +00:00
}
catch(err) {
self.logger.error(err.message);
}
});
2017-03-31 11:39:33 +00:00
filesFromPlugins.filter(function(file) {
2017-02-18 19:37:07 +00:00
if (utils.fileMatchesPattern(files, file.intendedPath)) {
2017-02-03 11:30:08 +00:00
readFiles.push(file);
}
});
2016-08-22 03:40:05 +00:00
return readFiles;
};
2017-03-31 11:39:33 +00:00
Config.prototype.loadPluginContractFiles = function() {
var self = this;
2017-01-26 11:34:00 +00:00
2017-03-31 11:39:33 +00:00
var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
2018-01-20 03:08:39 +00:00
contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./','');
self.contractsFiles.push(new File({filename: filename, type: 'custom', resolver: function(callback) {
callback(plugin.loadPluginFile(file));
}}));
2017-01-26 11:34:00 +00:00
});
2018-01-20 03:08:39 +00:00
});
2017-01-26 11:34:00 +00:00
};
2016-08-22 03:40:05 +00:00
module.exports = Config;