revert some of the recent changes

This commit is contained in:
Iuri Matias 2017-03-31 07:39:33 -04:00
parent faf0e1ff48
commit f4dbeb4b7e

View File

@ -1,11 +1,10 @@
let fs = require('./fs.js'); var fs = require('./fs.js');
let Plugins = require('./plugins.js'); var Plugins = require('./plugins.js');
let utils = require('../utils/utils.js'); var utils = require('../utils/utils.js');
// TODO: add wrapper for fs so it can also work in the browser // TODO: add wrapper for fs so it can also work in the browser
// can work with both read and save // can work with both read and save
class Config { var Config = function(options) {
constructor(options) {
this.env = options.env; this.env = options.env;
this.blockchainConfig = {}; this.blockchainConfig = {};
this.contractsConfig = {}; this.contractsConfig = {};
@ -19,18 +18,17 @@ class Config {
this.plugins = options.plugins; this.plugins = options.plugins;
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
} };
}
Config.prototype.loadConfigFiles = function (options) { Config.prototype.loadConfigFiles = function(options) {
let interceptLogs = options.interceptLogs; var interceptLogs = options.interceptLogs;
if (options.interceptLogs === undefined) { if (options.interceptLogs === undefined) {
interceptLogs = true; interceptLogs = true;
} }
//Check if the config file exists //Check if the config file exists
let embarkConfigExists = fs.existsSync(options.embarkConfig); var embarkConfigExists = fs.existsSync(options.embarkConfig);
if (!embarkConfigExists) { if(!embarkConfigExists){
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);
} }
@ -38,22 +36,23 @@ Config.prototype.loadConfigFiles = function (options) {
this.embarkConfig = fs.readJSONSync(options.embarkConfig); this.embarkConfig = fs.readJSONSync(options.embarkConfig);
this.embarkConfig.plugins = this.embarkConfig.plugins || {}; this.embarkConfig.plugins = this.embarkConfig.plugins || {};
this.plugins = new Plugins({ this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this});
plugins: this.embarkConfig.plugins,
logger: this.logger,
interceptLogs: interceptLogs,
events: this.events,
config: this
});
this.plugins.loadPlugins(); this.plugins.loadPlugins();
this.load(); this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
this.loadWebServerConfigFile(); this.loadWebServerConfigFile();
this.loadChainTrackerFile(); this.loadChainTrackerFile();
this.loadPluginContractFiles(); this.loadPluginContractFiles();
}; };
Config.prototype.load = Config.prototype.reloadConfig = function () { Config.prototype.reloadConfig = function() {
this.loadEmbarkConfigFile(); this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile(); this.loadBlockchainConfigFile();
this.loadStorageConfigFile(); this.loadStorageConfigFile();
@ -63,8 +62,8 @@ Config.prototype.load = Config.prototype.reloadConfig = function () {
this.loadChainTrackerFile(); this.loadChainTrackerFile();
}; };
Config.prototype.loadBlockchainConfigFile = function () { Config.prototype.loadBlockchainConfigFile = function() {
let defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json"); var defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json");
this.blockchainConfig = defaultBlockchainConfig[this.env] || {}; this.blockchainConfig = defaultBlockchainConfig[this.env] || {};
if (this.blockchainConfig.enabled === undefined) { if (this.blockchainConfig.enabled === undefined) {
@ -72,32 +71,31 @@ Config.prototype.loadBlockchainConfigFile = function () {
} }
}; };
Config.prototype.loadContractsConfigFile = function () { Config.prototype.loadContractsConfigFile = function() {
let configObject = {}; var configObject = {};
let configPlugins = [];
this.events.emit('get', 'contractsConfig', (kinds) => { var configPlugins = this.plugins.getPluginsFor('contractsConfig');
configPlugins = kinds;
});
if (configPlugins.length > 0) { if (configPlugins.length > 0) {
configPlugins.forEach(function (plugin) { configPlugins.forEach(function(plugin) {
plugin.contractsConfigs.forEach(function (pluginConfig) { plugin.contractsConfigs.forEach(function(pluginConfig) {
configObject = utils.recursiveMerge(configObject, pluginConfig); configObject = utils.recursiveMerge(configObject, pluginConfig);
}); });
}); });
} }
let contractsConfig = fs.readJSONSync(this.configDir + "contracts.json"); var contractsConfig = fs.readJSONSync(this.configDir + "contracts.json");
configObject = utils.recursiveMerge(configObject, contractsConfig); configObject = utils.recursiveMerge(configObject, contractsConfig);
let defaultContractsConfig = configObject['default']; var defaultContractsConfig = configObject['default'];
let envContractsConfig = configObject[this.env]; var envContractsConfig = configObject[this.env];
this.contractsConfig = utils.recursiveMerge(defaultContractsConfig, envContractsConfig); var mergedConfig = utils.recursiveMerge(defaultContractsConfig, envContractsConfig);
this.contractsConfig = mergedConfig;
}; };
Config.prototype.loadStorageConfigFile = function () { Config.prototype.loadStorageConfigFile = function() {
let configObject = { var configObject = {
"default": { "default": {
"enabled": true, "enabled": true,
"available_providers": ["ipfs"], "available_providers": ["ipfs"],
@ -106,10 +104,11 @@ Config.prototype.loadStorageConfigFile = function () {
"host": "localhost", "host": "localhost",
"port": 5001 "port": 5001
}, },
"development": {} "development": {
}
}; };
//let configPlugins = this.plugins.getPluginsFor('storageConfig'); //var configPlugins = this.plugins.getPluginsFor('storageConfig');
//if (configPlugins.length > 0) { //if (configPlugins.length > 0) {
// configPlugins.forEach(function(plugin) { // configPlugins.forEach(function(plugin) {
// plugin.contractsConfigs.forEach(function(pluginConfig) { // plugin.contractsConfigs.forEach(function(pluginConfig) {
@ -118,16 +117,16 @@ Config.prototype.loadStorageConfigFile = function () {
// }); // });
//} //}
let storageConfig; var storageConfig;
if (fs.existsSync(this.configDir + "storage.json")) { if (fs.existsSync(this.configDir + "storage.json")) {
storageConfig = fs.readJSONSync(this.configDir + "storage.json"); storageConfig = fs.readJSONSync(this.configDir + "storage.json");
configObject = utils.recursiveMerge(configObject, storageConfig); configObject = utils.recursiveMerge(configObject, storageConfig);
} }
let defaultStorageConfig = configObject['default']; var defaultStorageConfig = configObject['default'];
let envStorageConfig = configObject[this.env]; var envStorageConfig = configObject[this.env];
let mergedConfig = utils.recursiveMerge(defaultStorageConfig, envStorageConfig); var mergedConfig = utils.recursiveMerge(defaultStorageConfig, envStorageConfig);
this.storageConfig = mergedConfig || {}; this.storageConfig = mergedConfig || {};
if (this.storageConfig.enabled === undefined) { if (this.storageConfig.enabled === undefined) {
@ -138,8 +137,8 @@ Config.prototype.loadStorageConfigFile = function () {
} }
}; };
Config.prototype.loadCommunicationConfigFile = function () { Config.prototype.loadCommunicationConfigFile = function() {
let configObject = { var configObject = {
"default": { "default": {
"enabled": true, "enabled": true,
"provider": "whisper", "provider": "whisper",
@ -147,7 +146,7 @@ Config.prototype.loadCommunicationConfigFile = function () {
} }
}; };
//let configPlugins = this.plugins.getPluginsFor('communicationConfig'); //var configPlugins = this.plugins.getPluginsFor('communicationConfig');
//if (configPlugins.length > 0) { //if (configPlugins.length > 0) {
// configPlugins.forEach(function(plugin) { // configPlugins.forEach(function(plugin) {
// plugin.contractsConfigs.forEach(function(pluginConfig) { // plugin.contractsConfigs.forEach(function(pluginConfig) {
@ -156,17 +155,17 @@ Config.prototype.loadCommunicationConfigFile = function () {
// }); // });
//} //}
let communicationConfig; var communicationConfig;
if (fs.existsSync(this.configDir + "communication.json")) { if (fs.existsSync(this.configDir + "communication.json")) {
communicationConfig = fs.readJSONSync(this.configDir + "communication.json"); communicationConfig = fs.readJSONSync(this.configDir + "communication.json");
configObject = utils.recursiveMerge(configObject, communicationConfig); configObject = utils.recursiveMerge(configObject, communicationConfig);
} }
let defaultCommunicationConfig = configObject['default']; var defaultCommunicationConfig = configObject['default'];
let envCommunicationConfig = configObject[this.env]; var envCommunicationConfig = configObject[this.env];
let mergedConfig = utils.recursiveMerge(defaultCommunicationConfig, envCommunicationConfig); var mergedConfig = utils.recursiveMerge(defaultCommunicationConfig, envCommunicationConfig);
this.communicationConfig = mergedConfig || {}; this.communicationConfig = mergedConfig || {};
// TODO: probably not necessary if the default object is done right // TODO: probably not necessary if the default object is done right
@ -178,14 +177,14 @@ Config.prototype.loadCommunicationConfigFile = function () {
} }
}; };
Config.prototype.loadWebServerConfigFile = function () { Config.prototype.loadWebServerConfigFile = function() {
let webServerConfigJSON; var webServerConfigJSON;
if (fs.existsSync(this.configDir + "webserver.json")) { if (fs.existsSync(this.configDir + "webserver.json")) {
webServerConfigJSON = fs.readJSONSync(this.configDir + "webserver.json"); webServerConfigJSON = fs.readJSONSync(this.configDir + "webserver.json");
} else { } else {
webServerConfigJSON = {}; webServerConfigJSON = {};
} }
let defaultWebConfig = { var defaultWebConfig = {
"enabled": true, "enabled": true,
"host": "localhost", "host": "localhost",
"port": 8000 "port": 8000
@ -193,28 +192,28 @@ Config.prototype.loadWebServerConfigFile = function () {
this.webServerConfig = utils.recursiveMerge(defaultWebConfig, webServerConfigJSON); this.webServerConfig = utils.recursiveMerge(defaultWebConfig, webServerConfigJSON);
}; };
Config.prototype.loadEmbarkConfigFile = function () { Config.prototype.loadEmbarkConfigFile = function() {
let contracts = this.embarkConfig.contracts; var contracts = this.embarkConfig.contracts;
this.contractsFiles = this.loadFiles(contracts); this.contractsFiles = this.loadFiles(contracts);
this.buildDir = this.embarkConfig.buildDir; this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config; this.configDir = this.embarkConfig.config;
}; };
Config.prototype.loadPipelineConfigFile = function () { Config.prototype.loadPipelineConfigFile = function() {
let assets = this.embarkConfig.app; var assets = this.embarkConfig.app;
for (let targetFile in assets) { for(var targetFile in assets) {
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]); this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
} }
}; };
Config.prototype.loadChainTrackerFile = function () { Config.prototype.loadChainTrackerFile = function() {
//let self = this; //var self = this;
let chainTracker; var chainTracker;
try { try {
chainTracker = fs.readJSONSync(this.chainsFile); chainTracker = fs.readJSONSync(this.chainsFile);
} }
catch (err) { catch(err) {
//self.logger.info(this.chainsFile + ' file not found, creating it...'); //self.logger.info(this.chainsFile + ' file not found, creating it...');
chainTracker = {}; chainTracker = {};
fs.writeJSONSync(this.chainsFile, {}); fs.writeJSONSync(this.chainsFile, {});
@ -222,85 +221,65 @@ Config.prototype.loadChainTrackerFile = function () {
this.chainTracker = chainTracker; this.chainTracker = chainTracker;
}; };
Config.prototype.loadFiles = function (files) { Config.prototype.loadFiles = function(files) {
let self = this; var self = this;
let originalFiles = utils.filesMatchingPattern(files); var originalFiles = utils.filesMatchingPattern(files);
let readFiles = []; var readFiles = [];
// get embark.js object first // get embark.js object first
originalFiles.filter(function (file) { originalFiles.filter(function(file) {
return file.indexOf('.') >= 0; return file.indexOf('.') >= 0;
}).filter(function (file) { }).filter(function(file) {
if (file === 'embark.js') { if (file === 'embark.js') {
if (self.blockchainConfig.enabled || self.communicationConfig.provider === 'whisper' || self.communicationConfig.available_providers.indexOf('whisper') >= 0) { if (self.blockchainConfig.enabled || self.communicationConfig.provider === 'whisper' || self.communicationConfig.available_providers.indexOf('whisper') >= 0) {
readFiles.push({ readFiles.push({filename: 'web3.js', content: fs.readFileSync(fs.embarkPath("js/web3.js")).toString(), path: fs.embarkPath("js/web3.js")});
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)) { if (self.storageConfig.enabled && (self.storageConfig.provider === 'ipfs' || self.storageConfig.available_providers.indexOf('ipfs') >= 0)) {
readFiles.push({ readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(fs.embarkPath("js/ipfs.js")).toString(), path: fs.embarkPath("js/ipfs.js")});
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)) { 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 // TODO: remove duplicated files if functionality is the same for storage and orbit
readFiles.push({ 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")});
filename: 'ipfs-api.js', readFiles.push({filename: 'orbit.js', content: fs.readFileSync(fs.embarkPath("js/orbit.min.js")).toString(), path: fs.embarkPath("js/orbit.min.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")
});
} }
readFiles.push({ readFiles.push({filename: 'embark.js', content: fs.readFileSync(fs.embarkPath("js/build/embark.bundle.js")).toString(), path: fs.embarkPath("js/build/embark.bundle.js")});
filename: 'embark.js',
content: fs.readFileSync(fs.embarkPath("js/build/embark.bundle.js")).toString(),
path: fs.embarkPath("js/build/embark.bundle.js")
});
} }
}); });
// get plugins // get plugins
let filesFromPlugins = []; var filesFromPlugins = [];
let filePlugins = self.plugins.getPluginsFor('pipelineFiles'); var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
if (filePlugins.length > 0) { if (filePlugins.length > 0) {
filePlugins.forEach(function (plugin) { filePlugins.forEach(function(plugin) {
try { try {
let fileObjects = plugin.runFilePipeline(); var fileObjects = plugin.runFilePipeline();
for (let i = 0; i < fileObjects.length; i++) { for (var i=0; i < fileObjects.length; i++) {
let fileObject = fileObjects[i]; var fileObject = fileObjects[i];
filesFromPlugins.push(fileObject); filesFromPlugins.push(fileObject);
} }
} }
catch (err) { catch(err) {
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);
} }
}); });
// get user files // get user files
originalFiles.filter(function (file) { originalFiles.filter(function(file) {
return file.indexOf('.') >= 0; return file.indexOf('.') >= 0;
}).filter(function (file) { }).filter(function(file) {
if (file === 'embark.js') { if (file === 'embark.js') {
return; return;
} else if (file === 'abi.js') { } else if (file === 'abi.js') {
@ -313,19 +292,15 @@ Config.prototype.loadFiles = function (files) {
return readFiles; return readFiles;
}; };
Config.prototype.loadPluginContractFiles = function () { Config.prototype.loadPluginContractFiles = function() {
let self = this; var self = this;
let contractsPlugins = this.plugins.getPluginsFor('contractFiles'); var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
if (contractsPlugins.length > 0) { if (contractsPlugins.length > 0) {
contractsPlugins.forEach(function (plugin) { contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function (file) { plugin.contractsFiles.forEach(function(file) {
let filename = file.replace('./', ''); var filename = file.replace('./','');
self.contractsFiles.push({ self.contractsFiles.push({filename: filename, content: plugin.loadPluginFile(file), path: plugin.pathToFile(file)});
filename: filename,
content: plugin.loadPluginFile(file),
path: plugin.pathToFile(file)
});
}); });
}); });
} }