embark/lib/core/config.js

335 lines
10 KiB
JavaScript
Raw Normal View History

let fs = require('./fs.js');
let Plugins = require('./plugins.js');
let utils = require('../utils/utils.js');
2016-08-22 03:40:05 +00:00
2016-09-25 01:10:47 +00:00
// TODO: add wrapper for fs so it can also work in the browser
// can work with both read and save
2017-03-30 11:12:39 +00:00
class Config {
constructor(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-30 11:12:39 +00:00
Config.prototype.loadConfigFiles = function (options) {
let interceptLogs = options.interceptLogs;
2017-02-06 11:42:58 +00:00
if (options.interceptLogs === undefined) {
interceptLogs = true;
}
//Check if the config file exists
let embarkConfigExists = fs.existsSync(options.embarkConfig);
2017-03-30 11:12:39 +00:00
if (!embarkConfigExists) {
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-30 11:12:39 +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();
this.load();
2017-03-29 17:04:35 +00:00
this.loadWebServerConfigFile();
this.loadChainTrackerFile();
this.loadPluginContractFiles();
2016-08-22 03:40:05 +00:00
};
2017-03-30 11:12:39 +00:00
Config.prototype.load = Config.prototype.reloadConfig = function () {
this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
2016-09-25 01:10:47 +00:00
this.loadChainTrackerFile();
};
2016-08-22 03:40:05 +00:00
2017-03-30 11:12:39 +00:00
Config.prototype.loadBlockchainConfigFile = function () {
let defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json");
this.blockchainConfig = defaultBlockchainConfig[this.env] || {};
if (this.blockchainConfig.enabled === undefined) {
this.blockchainConfig.enabled = true;
}
2016-08-22 03:40:05 +00:00
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadContractsConfigFile = function () {
2017-01-26 11:34:00 +00:00
let configObject = {};
let configPlugins = [];
this.plugins.emit('get', 'contractsConfig', (kinds) => {
configPlugins = kinds;
});
2017-01-26 11:34:00 +00:00
if (configPlugins.length > 0) {
2017-03-30 11:12:39 +00:00
configPlugins.forEach(function (plugin) {
plugin.contractsConfigs.forEach(function (pluginConfig) {
2017-02-18 19:45:57 +00:00
configObject = utils.recursiveMerge(configObject, pluginConfig);
2017-01-26 11:34:00 +00:00
});
});
}
let contractsConfig = fs.readJSONSync(this.configDir + "contracts.json");
2017-02-18 19:45:57 +00:00
configObject = utils.recursiveMerge(configObject, contractsConfig);
let defaultContractsConfig = configObject['default'];
let envContractsConfig = configObject[this.env];
2016-08-22 03:40:05 +00:00
this.contractsConfig = utils.recursiveMerge(defaultContractsConfig, envContractsConfig);
2016-08-22 03:40:05 +00:00
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadStorageConfigFile = function () {
let configObject = {
"default": {
"enabled": true,
"available_providers": ["ipfs"],
"ipfs_bin": "ipfs",
"provider": "ipfs",
"host": "localhost",
"port": 5001
},
2017-03-30 11:12:39 +00:00
"development": {}
};
//let configPlugins = this.plugins.getPluginsFor('storageConfig');
//if (configPlugins.length > 0) {
// configPlugins.forEach(function(plugin) {
// plugin.contractsConfigs.forEach(function(pluginConfig) {
2017-02-18 19:45:57 +00:00
// configObject = utils.recursiveMerge(configObject, pluginConfig);
// });
// });
//}
let storageConfig;
if (fs.existsSync(this.configDir + "storage.json")) {
storageConfig = fs.readJSONSync(this.configDir + "storage.json");
configObject = utils.recursiveMerge(configObject, storageConfig);
}
let defaultStorageConfig = configObject['default'];
let envStorageConfig = configObject[this.env];
let 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 = [];
}
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadCommunicationConfigFile = function () {
let configObject = {
"default": {
"enabled": true,
"provider": "whisper",
"available_providers": ["whisper", "orbit"]
}
};
//let configPlugins = this.plugins.getPluginsFor('communicationConfig');
//if (configPlugins.length > 0) {
// configPlugins.forEach(function(plugin) {
// plugin.contractsConfigs.forEach(function(pluginConfig) {
2017-02-18 19:45:57 +00:00
// configObject = utils.recursiveMerge(configObject, pluginConfig);
// });
// });
//}
let communicationConfig;
if (fs.existsSync(this.configDir + "communication.json")) {
communicationConfig = fs.readJSONSync(this.configDir + "communication.json");
configObject = utils.recursiveMerge(configObject, communicationConfig);
}
let defaultCommunicationConfig = configObject['default'];
let envCommunicationConfig = configObject[this.env];
let 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 = [];
}
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadWebServerConfigFile = function () {
let webServerConfigJSON;
if (fs.existsSync(this.configDir + "webserver.json")) {
webServerConfigJSON = fs.readJSONSync(this.configDir + "webserver.json");
} else {
webServerConfigJSON = {};
}
let defaultWebConfig = {
"enabled": true,
"host": "localhost",
"port": 8000
};
this.webServerConfig = utils.recursiveMerge(defaultWebConfig, webServerConfigJSON);
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadEmbarkConfigFile = function () {
let contracts = this.embarkConfig.contracts;
2016-08-22 03:40:05 +00:00
this.contractsFiles = this.loadFiles(contracts);
2017-03-30 11:12:39 +00:00
this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config;
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadPipelineConfigFile = function () {
let assets = this.embarkConfig.app;
2017-03-30 11:12:39 +00:00
for (let targetFile in assets) {
2016-08-22 03:40:05 +00:00
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
}
};
2016-09-25 01:10:47 +00:00
2017-03-30 11:12:39 +00:00
Config.prototype.loadChainTrackerFile = function () {
//let self = this;
let chainTracker;
2016-09-25 01:10:47 +00:00
try {
2017-02-19 03:40:42 +00:00
chainTracker = fs.readJSONSync(this.chainsFile);
2016-09-25 01:10:47 +00:00
}
2017-03-30 11:12:39 +00:00
catch (err) {
//self.logger.info(this.chainsFile + ' file not found, creating it...');
2016-09-25 01:10:47 +00:00
chainTracker = {};
2017-02-19 03:40:42 +00:00
fs.writeJSONSync(this.chainsFile, {});
2016-09-25 01:10:47 +00:00
}
this.chainTracker = chainTracker;
};
2016-08-22 03:40:05 +00:00
2017-03-30 11:12:39 +00:00
Config.prototype.loadFiles = function (files) {
let self = this;
let originalFiles = utils.filesMatchingPattern(files);
let readFiles = [];
2016-08-22 03:40:05 +00:00
2017-02-03 11:30:08 +00:00
// get embark.js object first
2017-03-30 11:12:39 +00:00
originalFiles.filter(function (file) {
2016-08-22 03:40:05 +00:00
return file.indexOf('.') >= 0;
2017-03-30 11:12:39 +00:00
}).filter(function (file) {
2016-08-22 03:40:05 +00:00
if (file === 'embark.js') {
if (self.blockchainConfig.enabled || self.communicationConfig.provider === 'whisper' || self.communicationConfig.available_providers.indexOf('whisper') >= 0) {
2017-03-30 11:12:39 +00:00
readFiles.push({
filename: 'web3.js',
content: fs.readFileSync(fs.embarkPath("js/web3.js")).toString(),
path: fs.embarkPath("js/web3.js")
});
}
if (self.storageConfig.enabled && (self.storageConfig.provider === 'ipfs' || self.storageConfig.available_providers.indexOf('ipfs') >= 0)) {
2017-03-30 11:12:39 +00:00
readFiles.push({
filename: 'ipfs.js',
content: fs.readFileSync(fs.embarkPath("js/ipfs.js")).toString(),
path: fs.embarkPath("js/ipfs.js")
});
}
if (self.communicationConfig.enabled && (self.communicationConfig.provider === 'orbit' || self.communicationConfig.available_providers.indexOf('orbit') >= 0)) {
// TODO: remove duplicated files if functionality is the same for storage and orbit
2017-03-30 11:12:39 +00:00
readFiles.push({
filename: 'ipfs-api.js',
content: fs.readFileSync(fs.embarkPath("js/ipfs-api.min.js")).toString(),
path: fs.embarkPath("js/ipfs-api.min.js")
});
readFiles.push({
filename: 'orbit.js',
content: fs.readFileSync(fs.embarkPath("js/orbit.min.js")).toString(),
path: fs.embarkPath("js/orbit.min.js")
});
}
2017-03-30 11:12:39 +00:00
readFiles.push({
filename: 'embark.js',
content: fs.readFileSync(fs.embarkPath("js/build/embark.bundle.js")).toString(),
path: fs.embarkPath("js/build/embark.bundle.js")
});
2016-08-22 03:40:05 +00:00
}
});
2017-02-03 11:30:08 +00:00
// get plugins
let filesFromPlugins = [];
2017-02-03 11:30:08 +00:00
let filePlugins = self.plugins.getPluginsFor('pipelineFiles');
2017-02-03 11:30:08 +00:00
if (filePlugins.length > 0) {
2017-03-30 11:12:39 +00:00
filePlugins.forEach(function (plugin) {
2017-02-03 11:30:08 +00:00
try {
let fileObjects = plugin.runFilePipeline();
2017-03-30 11:12:39 +00:00
for (let i = 0; i < fileObjects.length; i++) {
let fileObject = fileObjects[i];
2017-02-03 11:30:08 +00:00
filesFromPlugins.push(fileObject);
}
}
2017-03-30 11:12:39 +00:00
catch (err) {
2017-02-03 11:30:08 +00:00
self.logger.error(err.message);
}
});
}
2017-03-30 11:12:39 +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);
}
});
// get user files
2017-03-30 11:12:39 +00:00
originalFiles.filter(function (file) {
2017-02-03 11:30:08 +00:00
return file.indexOf('.') >= 0;
2017-03-30 11:12:39 +00:00
}).filter(function (file) {
2017-02-03 11:30:08 +00:00
if (file === 'embark.js') {
return;
2017-02-16 00:27:23 +00:00
} else if (file === 'abi.js') {
readFiles.push({filename: file, content: "", path: file});
} else {
readFiles.push({filename: file, content: fs.readFileSync(file).toString(), path: file});
2017-02-03 11:30:08 +00:00
}
});
2016-08-22 03:40:05 +00:00
return readFiles;
};
2017-03-30 11:12:39 +00:00
Config.prototype.loadPluginContractFiles = function () {
let self = this;
2017-01-26 11:34:00 +00:00
let contractsPlugins = this.plugins.getPluginsFor('contractFiles');
2017-01-26 11:34:00 +00:00
if (contractsPlugins.length > 0) {
2017-03-30 11:12:39 +00:00
contractsPlugins.forEach(function (plugin) {
plugin.contractsFiles.forEach(function (file) {
let filename = file.replace('./', '');
self.contractsFiles.push({
filename: filename,
content: plugin.loadPluginFile(file),
path: plugin.pathToFile(file)
});
2017-01-26 11:34:00 +00:00
});
});
}
};
2016-08-22 03:40:05 +00:00
module.exports = Config;