2017-02-19 16:37:54 +00:00
|
|
|
var fs = require('./fs.js');
|
2017-01-26 11:34:00 +00:00
|
|
|
var Plugins = require('./plugins.js');
|
2017-02-18 19:10:01 +00:00
|
|
|
var utils = require('./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
|
2016-10-15 19:54:19 +00:00
|
|
|
var Config = function(options) {
|
|
|
|
this.env = options.env;
|
2016-08-22 03:40:05 +00:00
|
|
|
this.blockchainConfig = {};
|
|
|
|
this.contractsConfig = {};
|
|
|
|
this.pipelineConfig = {};
|
2016-09-25 01:10:47 +00:00
|
|
|
this.chainTracker = {};
|
2016-08-22 03:40:05 +00:00
|
|
|
this.assetFiles = {};
|
|
|
|
this.contractsFiles = [];
|
2016-10-15 19:54:19 +00:00
|
|
|
this.configDir = options.configDir || 'config/';
|
|
|
|
this.chainsFile = options.chainsFile || './chains.json';
|
2017-01-29 06:28:01 +00:00
|
|
|
this.plugins = options.plugins;
|
2017-01-26 11:34:00 +00:00
|
|
|
this.logger = options.logger;
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadConfigFiles = function(options) {
|
2017-02-06 11:42:58 +00:00
|
|
|
var interceptLogs = options.interceptLogs;
|
|
|
|
if (options.interceptLogs === undefined) {
|
|
|
|
interceptLogs = true;
|
|
|
|
}
|
2017-02-21 20:45:10 +00:00
|
|
|
|
|
|
|
//Check if the config file exists
|
|
|
|
var embarkConfigExists = fs.existsSync(options.embarkConfig);
|
|
|
|
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-02-06 11:42:58 +00:00
|
|
|
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs});
|
2017-01-26 11:34:00 +00:00
|
|
|
this.plugins.loadPlugins();
|
|
|
|
|
2017-03-01 02:26:15 +00:00
|
|
|
this.loadEmbarkConfigFile();
|
2017-02-03 11:30:08 +00:00
|
|
|
this.loadBlockchainConfigFile();
|
2017-02-10 12:44:06 +00:00
|
|
|
this.loadStorageConfigFile();
|
|
|
|
this.loadCommunicationConfigFile();
|
2017-02-03 11:30:08 +00:00
|
|
|
|
2017-03-01 02:26:15 +00:00
|
|
|
this.loadPipelineConfigFile();
|
|
|
|
|
2016-08-22 03:40:05 +00:00
|
|
|
this.loadContractsConfigFile();
|
2016-09-25 01:10:47 +00:00
|
|
|
this.loadChainTrackerFile();
|
2017-01-26 11:34:00 +00:00
|
|
|
this.loadPluginContractFiles();
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
2016-09-23 08:37:15 +00:00
|
|
|
|
|
|
|
Config.prototype.reloadConfig = function() {
|
2017-03-01 02:26:15 +00:00
|
|
|
this.loadEmbarkConfigFile();
|
2016-09-23 08:37:15 +00:00
|
|
|
this.loadBlockchainConfigFile();
|
2017-02-10 12:44:06 +00:00
|
|
|
this.loadStorageConfigFile();
|
|
|
|
this.loadCommunicationConfigFile();
|
2017-03-01 02:26:15 +00:00
|
|
|
this.loadPipelineConfigFile();
|
2016-09-23 08:37:15 +00:00
|
|
|
this.loadContractsConfigFile();
|
2016-09-25 01:10:47 +00:00
|
|
|
this.loadChainTrackerFile();
|
2016-09-23 08:37:15 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
|
|
|
Config.prototype.loadBlockchainConfigFile = function() {
|
2017-02-19 03:40:42 +00:00
|
|
|
var defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json");
|
|
|
|
this.blockchainConfig = defaultBlockchainConfig[this.env];
|
2017-03-01 02:26:15 +00:00
|
|
|
|
|
|
|
if (this.blockchainConfig.enabled === undefined) {
|
|
|
|
this.blockchainConfig.enabled = true;
|
|
|
|
}
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadContractsConfigFile = function() {
|
2017-01-26 11:34:00 +00:00
|
|
|
|
|
|
|
var configObject = {};
|
|
|
|
|
|
|
|
var configPlugins = this.plugins.getPluginsFor('contractsConfig');
|
|
|
|
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);
|
2017-01-26 11:34:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-19 03:40:42 +00:00
|
|
|
var contractsConfig = fs.readJSONSync(this.configDir + "contracts.json");
|
2017-02-18 19:45:57 +00:00
|
|
|
configObject = utils.recursiveMerge(configObject, contractsConfig);
|
2017-01-26 11:34:00 +00:00
|
|
|
var defaultContractsConfig = configObject['default'];
|
|
|
|
var envContractsConfig = configObject[this.env];
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-02-18 19:45:57 +00:00
|
|
|
var mergedConfig = utils.recursiveMerge(defaultContractsConfig, envContractsConfig);
|
2016-10-15 19:54:19 +00:00
|
|
|
this.contractsConfig = mergedConfig;
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
2017-02-10 12:44:06 +00:00
|
|
|
|
|
|
|
Config.prototype.loadStorageConfigFile = function() {
|
|
|
|
var configObject = {};
|
|
|
|
|
|
|
|
//var 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);
|
2017-02-10 12:44:06 +00:00
|
|
|
// });
|
|
|
|
// });
|
|
|
|
//}
|
|
|
|
|
2017-02-19 03:40:42 +00:00
|
|
|
var storageConfig = fs.readJSONSync(this.configDir + "storage.json");
|
2017-02-18 19:45:57 +00:00
|
|
|
configObject = utils.recursiveMerge(configObject, storageConfig);
|
2017-02-10 12:44:06 +00:00
|
|
|
var defaultStorageConfig = configObject['default'];
|
|
|
|
var envStorageConfig = configObject[this.env];
|
|
|
|
|
2017-02-18 19:45:57 +00:00
|
|
|
var mergedConfig = utils.recursiveMerge(defaultStorageConfig, envStorageConfig);
|
2017-02-10 12:44:06 +00:00
|
|
|
this.storageConfig = mergedConfig;
|
2017-03-01 02:26:15 +00:00
|
|
|
|
|
|
|
if (this.storageConfig.enabled === undefined) {
|
|
|
|
this.storageConfig.enabled = true;
|
|
|
|
}
|
2017-02-10 12:44:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadCommunicationConfigFile = function() {
|
|
|
|
var configObject = {};
|
|
|
|
|
|
|
|
//var 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);
|
2017-02-10 12:44:06 +00:00
|
|
|
// });
|
|
|
|
// });
|
|
|
|
//}
|
|
|
|
|
2017-02-19 03:40:42 +00:00
|
|
|
var communicationConfig = fs.readJSONSync(this.configDir + "communication.json");
|
2017-02-18 19:45:57 +00:00
|
|
|
configObject = utils.recursiveMerge(configObject, communicationConfig);
|
2017-02-10 12:44:06 +00:00
|
|
|
var defaultCommunicationConfig = configObject['default'];
|
|
|
|
var envCommunicationConfig = configObject[this.env];
|
|
|
|
|
2017-02-18 19:45:57 +00:00
|
|
|
var mergedConfig = utils.recursiveMerge(defaultCommunicationConfig, envCommunicationConfig);
|
2017-02-10 12:44:06 +00:00
|
|
|
this.communicationConfig = mergedConfig;
|
2017-03-01 02:26:15 +00:00
|
|
|
|
|
|
|
if (this.communicationConfig.enabled === undefined) {
|
|
|
|
this.communicationConfig.enabled = true;
|
|
|
|
}
|
2017-02-10 12:44:06 +00:00
|
|
|
};
|
|
|
|
|
2017-03-01 02:26:15 +00:00
|
|
|
Config.prototype.loadEmbarkConfigFile = function() {
|
2016-08-22 03:40:05 +00:00
|
|
|
var contracts = this.embarkConfig.contracts;
|
|
|
|
this.contractsFiles = this.loadFiles(contracts);
|
|
|
|
|
2017-03-01 02:26:15 +00:00
|
|
|
this.buildDir = this.embarkConfig.buildDir;
|
|
|
|
this.configDir = this.embarkConfig.config;
|
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadPipelineConfigFile = function() {
|
2016-08-22 03:40:05 +00:00
|
|
|
var assets = this.embarkConfig.app;
|
|
|
|
for(var targetFile in assets) {
|
|
|
|
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
|
|
|
|
}
|
|
|
|
};
|
2016-09-25 01:10:47 +00:00
|
|
|
|
|
|
|
Config.prototype.loadChainTrackerFile = function() {
|
|
|
|
//var self = this;
|
|
|
|
var chainTracker;
|
|
|
|
try {
|
2017-02-19 03:40:42 +00:00
|
|
|
chainTracker = fs.readJSONSync(this.chainsFile);
|
2016-09-25 01:10:47 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
2016-10-15 19:54:19 +00:00
|
|
|
//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
|
|
|
|
|
|
|
Config.prototype.loadFiles = function(files) {
|
2017-02-03 11:30:08 +00:00
|
|
|
var self = this;
|
2017-02-18 19:45:57 +00:00
|
|
|
var originalFiles = utils.filesMatchingPattern(files);
|
2016-08-22 03:40:05 +00:00
|
|
|
var readFiles = [];
|
|
|
|
|
2017-02-03 11:30:08 +00:00
|
|
|
// get embark.js object first
|
2016-08-22 03:40:05 +00:00
|
|
|
originalFiles.filter(function(file) {
|
|
|
|
return file.indexOf('.') >= 0;
|
|
|
|
}).filter(function(file) {
|
|
|
|
if (file === 'embark.js') {
|
2017-03-01 02:26:15 +00:00
|
|
|
|
|
|
|
if (self.blockchainConfig.enabled || self.communicationConfig.provider === 'whisper') {
|
|
|
|
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') {
|
|
|
|
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') {
|
|
|
|
// TODO: remove duplicated files if functionality is the same for storage and orbit
|
|
|
|
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-02-20 00:44:16 +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
|
|
|
|
var filesFromPlugins = [];
|
|
|
|
|
|
|
|
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
|
|
|
|
|
|
|
|
if (filePlugins.length > 0) {
|
|
|
|
filePlugins.forEach(function(plugin) {
|
|
|
|
try {
|
|
|
|
var fileObjects = plugin.runFilePipeline();
|
|
|
|
for (var i=0; i < fileObjects.length; i++) {
|
|
|
|
var fileObject = fileObjects[i];
|
|
|
|
filesFromPlugins.push(fileObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
originalFiles.filter(function(file) {
|
|
|
|
return file.indexOf('.') >= 0;
|
|
|
|
}).filter(function(file) {
|
|
|
|
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-01-26 11:34:00 +00:00
|
|
|
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)});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-22 03:40:05 +00:00
|
|
|
module.exports = Config;
|