embark/lib/core/config.js
Eric Mastro 2bb977df76 WIP to merge in other swarm changes
Adding swarm to embarkjs. WIP.

Add 'auto' setting for geth CORS and websockets origin

* 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config.
* 'auto' set to the default value in blockchain config for test and demo apps.
test add config and contract and add test

addFileToPipeline test and registerBeforeDeploy with new arg

add more registers but generation one fails in run

WIP commit

Undo changes to test config.

Merge pull request #381 from embark-framework/features/cors-auto

Add 'auto' setting for geth CORS and websockets origin
fix a bug where upload cmd used plugin name

don't error if it's an empty dapp with no contracts yet

Merge pull request #383 from embark-framework/no_contracts

don't error if it's an empty dapp with no contracts yet
remove duplicated entry

force zepplein version for travis

Merge pull request #384 from embark-framework/chores/test-allpligin-apis

Small fixes for plugin APIs
intercept logs in the app itself - stopgap fix

Merge pull request #385 from embark-framework/console_logs_fix

intercept logs in the app itself - stopgap fix
* removed unneeded provider property.
* add 'swarm' as a provider in the storage.config
* update method for swarm service check

Merge branch 'develop' into features/add-swarm-to-embarkjs


More work to add swarm to embarkjs

* added eth-lib to parse result of swarm text
* changed "currentStorage" and "currentMessages" to "currentProvider" for consistency.
* added protocol to storage config
* selectively starts storage service depending on which one is configured in the storage config
* run service check for ipfs/swarm prior to uploaded
* added swarm methods for embarkjs

Updated code based on code review

check if testrpc is installed and warn if not

Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed

check if testrpc is installed and warn if not
Removed timeout

Removed spacer

Merge pull request #382 from embark-framework/react-demo

Updating embark demo to use react instead of jquery
fix on contract add

Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp

Fix adding a contract
redeploy with right config on config change

fix tests

reset watchers after build to make sure files remain watch

Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched

Fix files not being watched
Merge pull request #388 from embark-framework/bug_fix/changing-contract-config

Redeploy with right config on config change
Added swarm support in embarkjs and isAvailable for messages/storage

* reverted currentProvider back to currentStorage and currentMessages
* added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm
* Finished swarm implementation in embarkjs plus cleanup
* updated test app storage config to swarm to show swarm config option

Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 15:56:43 +10:00

325 lines
10 KiB
JavaScript

const fs = require('./fs.js');
const File = require('./file.js');
const Plugins = require('./plugins.js');
const utils = require('../utils/utils.js');
const path = require('path');
const deepEqual = require('deep-equal');
const constants = require('../constants');
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;
this.embarkConfig = {};
this.context = options.context || [constants.contexts.any];
};
Config.prototype.loadConfigFiles = function(options) {
var interceptLogs = options.interceptLogs;
if (options.interceptLogs === undefined) {
interceptLogs = true;
}
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);
}
this.embarkConfig = fs.readJSONSync(options.embarkConfig);
this.embarkConfig.plugins = this.embarkConfig.plugins || {};
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context});
this.plugins.loadPlugins();
this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadContractsConfigFile();
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
this.loadExternalContractsFiles();
this.loadWebServerConfigFile();
this.loadChainTrackerFile();
this.loadPluginContractFiles();
};
Config.prototype.reloadConfig = function() {
this.loadEmbarkConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadContractsConfigFile();
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
this.loadExternalContractsFiles();
this.loadChainTrackerFile();
};
Config.prototype._mergeConfig = function(configFilePath, defaultConfig, env, enabledByDefault) {
if (!configFilePath) {
let configToReturn = defaultConfig['default'] || {};
configToReturn.enabled = enabledByDefault || false;
return configToReturn;
}
if (!fs.existsSync(configFilePath)) {
// TODO: remove this if
if (this.logger) {
this.logger.warn("no config file found at " + configFilePath + ". using default config");
}
return defaultConfig['default'] || {};
}
let config = fs.readJSONSync(configFilePath);
let configObject = utils.recursiveMerge(defaultConfig, config);
if (env) {
return utils.recursiveMerge(configObject['default'] || {}, configObject[env]);
} else {
return configObject;
}
};
Config.prototype._getFileOrOject = function(object, filePath, property) {
if (typeof (this.configDir) === 'object') {
return this.configDir[property];
}
return this.configDir + filePath;
};
Config.prototype.loadBlockchainConfigFile = function() {
var configObject = {
"default": {
"enabled": true,
"rpcCorsDomain": "auto",
"wsOrigins": "auto"
}
};
let configFilePath = this._getFileOrOject(this.configDir, 'blockchain.json', 'blockchain');
this.blockchainConfig = this._mergeConfig(configFilePath, configObject, this.env, true);
};
Config.prototype.loadContractsConfigFile = function() {
var defaultVersions = {
"web3": "1.0.0-beta",
"solc": "0.4.17"
};
var versions = utils.recursiveMerge(defaultVersions, this.embarkConfig.versions || {});
var configObject = {
"default": {
"versions": versions,
"deployment": {
"host": "localhost", "port": 8545, "type": "rpc"
},
"dappConnection": [
"$WEB3",
"localhost:8545"
],
"gas": "auto",
"contracts": {
}
}
};
var contractsConfigs = this.plugins.getPluginsProperty('contractsConfig', 'contractsConfigs');
contractsConfigs.forEach(function(pluginConfig) {
configObject = utils.recursiveMerge(configObject, pluginConfig);
});
let configFilePath = this._getFileOrOject(this.configDir, 'contracts.json', 'contracts');
const newContractsConfig = this._mergeConfig(configFilePath, configObject, this.env);
if (!deepEqual(newContractsConfig, this.contractsConfig)) {
this.events.emit(constants.events.contractConfigChanged, newContractsConfig);
this.contractsConfig = newContractsConfig;
}
};
Config.prototype.loadExternalContractsFiles = function() {
let contracts = this.contractsConfig.contracts;
for (let contractName in contracts) {
let contract = contracts[contractName];
if (!contract.file) {
continue;
}
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
const fileObj = utils.getExternalContractUrl(contract.file);
if (!fileObj) {
return this.logger.error("HTTP contract file not found: " + contract.file);
}
const localFile = fileObj.filePath;
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: fileObj.url}));
} else if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file}));
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: File.types.dapp_file, basedir: '', path: path.join('./node_modules/', contract.file)}));
} else {
this.logger.error("contract file not found: " + contract.file);
}
}
};
Config.prototype.loadStorageConfigFile = function() {
var versions = utils.recursiveMerge({"ipfs-api": "17.2.4"}, this.embarkConfig.versions || {});
var configObject = {
"default": {
"versions": versions,
"enabled": true,
"available_providers": ["ipfs", "swarm"],
"ipfs_bin": "ipfs",
"provider": "ipfs",
"protocol": "http",
"host": "localhost",
"port": 5001,
"getUrl": "http://localhost:8080/ipfs/"
}
};
let configFilePath = this._getFileOrOject(this.configDir, 'storage.json', 'storage');
this.storageConfig = this._mergeConfig(configFilePath, configObject, this.env);
};
Config.prototype.loadCommunicationConfigFile = function() {
var configObject = {
"default": {
"enabled": true,
"provider": "whisper",
"available_providers": ["whisper", "orbit"],
"connection": {
"host": "localhost", "port": 8546, "type": "ws"
}
}
};
let configFilePath = this._getFileOrOject(this.configDir, 'communication.json', 'communication');
this.communicationConfig = this._mergeConfig(configFilePath, configObject, this.env);
};
Config.prototype.loadWebServerConfigFile = function() {
var configObject = {
"enabled": true, "host": "localhost", "port": 8000
};
let configFilePath = this._getFileOrOject(this.configDir, 'webserver.json', 'webserver');
this.webServerConfig = this._mergeConfig(configFilePath, configObject, false);
};
Config.prototype.loadEmbarkConfigFile = function() {
const contracts = this.embarkConfig.contracts;
const newContractsFiles = this.loadFiles(contracts);
if (!this.contractFiles || newContractsFiles.length !== this.contractFiles.length || !deepEqual(newContractsFiles, this.contractFiles)) {
this.events.emit(constants.events.contractFilesChanged, newContractsFiles);
this.contractsFiles = newContractsFiles;
}
// determine contract 'root' directories
this.contractDirectories = contracts.map((dir) => {
return dir.split("**")[0];
}).map((dir) => {
return dir.split("*.")[0];
});
this.contractDirectories.push(constants.httpContractsDirectory);
this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config;
};
Config.prototype.loadPipelineConfigFile = function() {
var assets = this.embarkConfig.app;
for(var targetFile in assets) {
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
}
};
Config.prototype.loadChainTrackerFile = function() {
if (!fs.existsSync(this.chainsFile)) {
this.logger.info(this.chainsFile + ' file not found, creating it...');
fs.writeJSONSync(this.chainsFile, {});
}
this.chainTracker = fs.readJSONSync(this.chainsFile);
};
function findMatchingExpression(filename, filesExpressions) {
for (let fileExpression of filesExpressions) {
var matchingFiles = utils.filesMatchingPattern(fileExpression);
for (let matchFile of matchingFiles) {
if (matchFile === filename) {
return path.dirname(fileExpression).replace(/\*/g, '');
}
}
}
return path.dirname(filename);
}
Config.prototype.loadFiles = function(files) {
var self = this;
var originalFiles = utils.filesMatchingPattern(files);
var readFiles = [];
originalFiles.filter(function(file) {
return (file[0] === '$' || file.indexOf('.') >= 0);
}).filter(function(file) {
let basedir = findMatchingExpression(file, files);
readFiles.push(new File({filename: file, type: File.types.dapp_file, basedir: basedir, path: file}));
});
var filesFromPlugins = [];
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
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) {
if ((file.intendedPath && utils.fileMatchesPattern(files, file.intendedPath)) || utils.fileMatchesPattern(files, file.file)) {
readFiles.push(file);
}
});
return readFiles;
};
Config.prototype.loadPluginContractFiles = function() {
var self = this;
var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./','');
self.contractsFiles.push(new File({filename: filename, type: File.types.custom, path: filename, resolver: function(callback) {
callback(plugin.loadPluginFile(file));
}}));
});
});
};
module.exports = Config;