2016-08-22 03:40:05 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var grunt = require('grunt');
|
2016-10-15 19:54:19 +00:00
|
|
|
var merge = require('merge');
|
2016-10-29 17:50:54 +00:00
|
|
|
var path = require('path');
|
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';
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadConfigFiles = function(options) {
|
|
|
|
this.embarkConfig = JSON.parse(fs.readFileSync(options.embarkConfig));
|
|
|
|
|
|
|
|
this.loadPipelineConfigFile();
|
|
|
|
this.loadBlockchainConfigFile();
|
|
|
|
this.loadContractsConfigFile();
|
2016-09-25 01:10:47 +00:00
|
|
|
this.loadChainTrackerFile();
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
2016-09-23 08:37:15 +00:00
|
|
|
|
|
|
|
Config.prototype.reloadConfig = function() {
|
|
|
|
this.loadPipelineConfigFile();
|
|
|
|
this.loadBlockchainConfigFile();
|
|
|
|
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() {
|
2016-09-25 06:23:33 +00:00
|
|
|
var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + "blockchain.json"))[this.env];
|
2016-08-22 03:40:05 +00:00
|
|
|
this.blockchainConfig = defaultBlockchainConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadContractsConfigFile = function() {
|
2016-10-15 19:54:19 +00:00
|
|
|
var contractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json"));
|
|
|
|
var defaultContractsConfig = contractsConfig['default'];
|
|
|
|
var envContractsConfig = contractsConfig[this.env];
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2016-10-15 19:54:19 +00:00
|
|
|
var mergedConfig = merge.recursive(defaultContractsConfig, envContractsConfig);
|
|
|
|
this.contractsConfig = mergedConfig;
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadPipelineConfigFile = function() {
|
|
|
|
var contracts = this.embarkConfig.contracts;
|
|
|
|
this.contractsFiles = this.loadFiles(contracts);
|
|
|
|
|
|
|
|
var assets = this.embarkConfig.app;
|
|
|
|
for(var targetFile in assets) {
|
|
|
|
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buildDir = this.embarkConfig.buildDir;
|
|
|
|
this.configDir = this.embarkConfig.config;
|
|
|
|
};
|
2016-09-25 01:10:47 +00:00
|
|
|
|
|
|
|
Config.prototype.loadChainTrackerFile = function() {
|
|
|
|
//var self = this;
|
|
|
|
var chainTracker;
|
|
|
|
try {
|
2016-10-15 19:54:19 +00:00
|
|
|
chainTracker = JSON.parse(fs.readFileSync(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 = {};
|
2016-10-15 19:54:19 +00:00
|
|
|
fs.writeFileSync(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) {
|
|
|
|
var originalFiles = grunt.file.expand({nonull: true}, files);
|
|
|
|
var readFiles = [];
|
|
|
|
|
|
|
|
originalFiles.filter(function(file) {
|
|
|
|
return file.indexOf('.') >= 0;
|
|
|
|
}).filter(function(file) {
|
|
|
|
if (file === 'embark.js') {
|
2016-10-29 17:50:54 +00:00
|
|
|
readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString()});
|
|
|
|
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString()});
|
2017-01-07 05:03:03 +00:00
|
|
|
// TODO: remove duplicated files if funcitonality is the same for storage and orbit
|
|
|
|
readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs-api.min.js")).toString()});
|
|
|
|
readFiles.push({filename: 'orbit.js', content: fs.readFileSync(path.join(__dirname, "/../js/orbit.min.js")).toString()});
|
2016-10-29 17:50:54 +00:00
|
|
|
readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString()});
|
2016-08-22 03:40:05 +00:00
|
|
|
} else {
|
|
|
|
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return readFiles;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Config;
|