2018-04-17 19:07:00 +00:00
|
|
|
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');
|
2018-04-30 05:56:43 +00:00
|
|
|
const deepEqual = require('deep-equal');
|
2018-09-25 09:27:41 +00:00
|
|
|
const web3 = require('web3');
|
2018-04-19 18:26:11 +00:00
|
|
|
const constants = require('../constants');
|
2018-07-15 20:33:48 +00:00
|
|
|
const {canonicalHost, defaultHost} = require('../utils/host');
|
2018-04-18 16:59:58 +00:00
|
|
|
|
2018-10-03 14:02:29 +00:00
|
|
|
const DEFAULT_CONFIG_PATH = 'config/';
|
2018-09-25 09:27:41 +00:00
|
|
|
const unitRegex = /([0-9]+) ([a-zA-Z]+)/;
|
2018-10-03 14:02:29 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
var Config = function(options) {
|
2018-06-14 13:21:51 +00:00
|
|
|
const self = this;
|
2018-10-10 15:34:56 +00:00
|
|
|
this.env = options.env || 'default';
|
2017-03-31 11:39:33 +00:00
|
|
|
this.blockchainConfig = {};
|
|
|
|
this.contractsConfig = {};
|
|
|
|
this.pipelineConfig = {};
|
2018-07-16 04:49:24 +00:00
|
|
|
this.webServerConfig = options.webServerConfig;
|
2017-03-31 11:39:33 +00:00
|
|
|
this.chainTracker = {};
|
|
|
|
this.assetFiles = {};
|
|
|
|
this.contractsFiles = [];
|
2018-10-04 09:40:52 +00:00
|
|
|
this.configDir = options.configDir || DEFAULT_CONFIG_PATH;
|
2017-03-31 11:39:33 +00:00
|
|
|
this.chainsFile = options.chainsFile || './chains.json';
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
2018-03-31 23:35:20 +00:00
|
|
|
this.embarkConfig = {};
|
2018-04-25 14:34:17 +00:00
|
|
|
this.context = options.context || [constants.contexts.any];
|
2018-09-27 10:57:42 +00:00
|
|
|
this.shownNoAccountConfigMsg = false; // flag to ensure "no account config" message is only displayed once to the user
|
2018-06-14 13:21:51 +00:00
|
|
|
|
|
|
|
self.events.setCommandHandler("config:contractsConfig", (cb) => {
|
|
|
|
cb(self.contractsConfig);
|
|
|
|
});
|
|
|
|
|
2018-10-10 20:34:04 +00:00
|
|
|
self.events.setCommandHandler("config:contractsConfig:set", (config, cb) => {
|
|
|
|
self.contractsConfig = config;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
|
2018-06-14 13:21:51 +00:00
|
|
|
self.events.setCommandHandler("config:contractsFiles", (cb) => {
|
|
|
|
cb(self.contractsFiles);
|
|
|
|
});
|
2018-06-18 15:25:43 +00:00
|
|
|
|
2018-06-18 15:37:23 +00:00
|
|
|
// TODO: refactor this so reading the file can be done with a normal resolver or something that takes advantage of the plugin api
|
2018-06-18 15:25:43 +00:00
|
|
|
self.events.setCommandHandler("config:contractsFiles:add", (filename) => {
|
|
|
|
self.contractsFiles.push(new File({filename: filename, type: File.types.custom, path: filename, resolver: function(callback) {
|
|
|
|
callback(fs.readFileSync(filename).toString());
|
|
|
|
}}));
|
|
|
|
});
|
2017-03-31 11:39:33 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadConfigFiles = function(options) {
|
|
|
|
var interceptLogs = options.interceptLogs;
|
2017-02-06 11:42:58 +00:00
|
|
|
if (options.interceptLogs === undefined) {
|
|
|
|
interceptLogs = true;
|
|
|
|
}
|
2017-02-21 20:45:10 +00:00
|
|
|
|
2018-01-20 03:08:39 +00:00
|
|
|
if (!fs.existsSync(options.embarkConfig)){
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.error(__('Cannot find file %s Please ensure you are running this command inside the Dapp folder', options.embarkConfig));
|
2017-02-21 20:45:10 +00:00
|
|
|
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
|
|
|
|
2018-05-19 02:40:47 +00:00
|
|
|
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context, env: this.env});
|
2017-01-26 11:34:00 +00:00
|
|
|
this.plugins.loadPlugins();
|
|
|
|
|
2018-08-20 18:03:10 +00:00
|
|
|
this.loadEmbarkConfigFile();
|
2017-03-31 11:39:33 +00:00
|
|
|
this.loadBlockchainConfigFile();
|
|
|
|
this.loadStorageConfigFile();
|
|
|
|
this.loadCommunicationConfigFile();
|
2018-05-25 17:25:02 +00:00
|
|
|
this.loadNameSystemConfigFile();
|
2017-03-31 11:39:33 +00:00
|
|
|
this.loadPipelineConfigFile();
|
2018-10-04 09:40:52 +00:00
|
|
|
this.loadAssetFiles();
|
2017-03-31 11:39:33 +00:00
|
|
|
this.loadContractsConfigFile();
|
2018-04-12 21:54:08 +00:00
|
|
|
this.loadExternalContractsFiles();
|
2017-03-29 17:04:35 +00:00
|
|
|
this.loadWebServerConfigFile();
|
|
|
|
this.loadChainTrackerFile();
|
|
|
|
this.loadPluginContractFiles();
|
2018-05-25 01:00:26 +00:00
|
|
|
|
|
|
|
this._updateBlockchainCors();
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
2016-09-23 08:37:15 +00:00
|
|
|
|
2017-03-31 11:39:33 +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();
|
2018-05-25 17:25:02 +00:00
|
|
|
this.loadNameSystemConfigFile();
|
2017-03-01 02:26:15 +00:00
|
|
|
this.loadPipelineConfigFile();
|
2018-10-04 09:40:52 +00:00
|
|
|
this.loadAssetFiles();
|
2016-09-23 08:37:15 +00:00
|
|
|
this.loadContractsConfigFile();
|
2018-04-12 21:54:08 +00:00
|
|
|
this.loadExternalContractsFiles();
|
2016-09-25 01:10:47 +00:00
|
|
|
this.loadChainTrackerFile();
|
2018-05-25 01:00:26 +00:00
|
|
|
|
|
|
|
this._updateBlockchainCors();
|
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype._updateBlockchainCors = function(){
|
|
|
|
let blockchainConfig = this.blockchainConfig;
|
|
|
|
let storageConfig = this.storageConfig;
|
|
|
|
let webServerConfig = this.webServerConfig;
|
2018-05-31 10:18:25 +00:00
|
|
|
let corsParts = [];
|
2018-05-25 01:00:26 +00:00
|
|
|
|
|
|
|
if(webServerConfig && webServerConfig.enabled) {
|
2018-06-01 03:12:17 +00:00
|
|
|
if(webServerConfig.host) corsParts.push(utils.buildUrlFromConfig(webServerConfig));
|
2018-05-25 01:00:26 +00:00
|
|
|
}
|
|
|
|
if(storageConfig && storageConfig.enabled) {
|
2018-05-31 10:18:25 +00:00
|
|
|
// if getUrl is specified in the config, that needs to be included in cors
|
|
|
|
// instead of the concatenated protocol://host:port
|
|
|
|
if(storageConfig.upload.getUrl) {
|
|
|
|
// remove /ipfs or /bzz: from getUrl if it's there
|
|
|
|
let getUrlParts = storageConfig.upload.getUrl.split('/');
|
|
|
|
getUrlParts = getUrlParts.slice(0, 3);
|
2018-07-15 20:33:48 +00:00
|
|
|
let host = canonicalHost(getUrlParts[2].split(':')[0]);
|
|
|
|
let port = getUrlParts[2].split(':')[1];
|
|
|
|
getUrlParts[2] = port ? [host, port].join(':') : host;
|
2018-05-31 10:18:25 +00:00
|
|
|
corsParts.push(getUrlParts.join('/'));
|
|
|
|
}
|
|
|
|
// use our modified getUrl or in case it wasn't specified, use a built url
|
|
|
|
else{
|
2018-06-01 03:12:17 +00:00
|
|
|
corsParts.push(utils.buildUrlFromConfig(storageConfig.upload));
|
2018-05-31 10:18:25 +00:00
|
|
|
}
|
2018-05-25 01:00:26 +00:00
|
|
|
}
|
2018-06-20 06:19:25 +00:00
|
|
|
// add whisper cors
|
|
|
|
if(this.communicationConfig && this.communicationConfig.enabled && this.communicationConfig.provider === 'whisper'){
|
2018-10-06 16:05:37 +00:00
|
|
|
corsParts.push('http://embark');
|
2018-06-20 06:19:25 +00:00
|
|
|
}
|
2018-05-25 01:00:26 +00:00
|
|
|
|
2018-05-31 10:18:25 +00:00
|
|
|
let cors = corsParts.join(',');
|
2018-08-17 17:38:19 +00:00
|
|
|
if(blockchainConfig.rpcCorsDomain === 'auto'){
|
2018-06-26 03:34:52 +00:00
|
|
|
if(cors.length) blockchainConfig.rpcCorsDomain = cors;
|
|
|
|
else blockchainConfig.rpcCorsDomain = '';
|
|
|
|
}
|
2018-08-17 17:38:19 +00:00
|
|
|
if(blockchainConfig.wsOrigins === 'auto'){
|
2018-06-26 03:34:52 +00:00
|
|
|
if(cors.length) blockchainConfig.wsOrigins = cors;
|
|
|
|
else blockchainConfig.wsOrigins = '';
|
|
|
|
}
|
2016-09-23 08:37:15 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-04-02 18:44:55 +00:00
|
|
|
Config.prototype._mergeConfig = function(configFilePath, defaultConfig, env, enabledByDefault) {
|
2018-04-01 01:06:00 +00:00
|
|
|
if (!configFilePath) {
|
2018-04-01 01:15:53 +00:00
|
|
|
let configToReturn = defaultConfig['default'] || {};
|
2018-04-02 18:44:55 +00:00
|
|
|
configToReturn.enabled = enabledByDefault || false;
|
2018-04-01 01:08:25 +00:00
|
|
|
return configToReturn;
|
2018-04-01 01:06:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:09:38 +00:00
|
|
|
// due to embark.json; TODO: refactor this
|
2018-06-12 21:59:14 +00:00
|
|
|
configFilePath = configFilePath.replace('.json','').replace('.js', '');
|
2018-05-11 22:22:03 +00:00
|
|
|
if (!fs.existsSync(configFilePath + '.js') && !fs.existsSync(configFilePath + '.json')) {
|
2018-04-01 01:15:53 +00:00
|
|
|
// TODO: remove this if
|
|
|
|
if (this.logger) {
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.warn(__("no config file found at %s using default config", configFilePath));
|
2018-04-01 01:15:53 +00:00
|
|
|
}
|
2018-01-20 03:08:39 +00:00
|
|
|
return defaultConfig['default'] || {};
|
2017-12-16 13:48:37 +00:00
|
|
|
}
|
2017-03-01 02:26:15 +00:00
|
|
|
|
2018-05-11 22:10:48 +00:00
|
|
|
let config;
|
|
|
|
if (fs.existsSync(configFilePath + '.js')) {
|
2018-09-03 18:01:46 +00:00
|
|
|
delete require.cache[fs.dappPath(configFilePath + '.js')];
|
2018-05-11 22:10:48 +00:00
|
|
|
config = require(fs.dappPath(configFilePath + '.js'));
|
|
|
|
} else {
|
2018-05-11 22:22:03 +00:00
|
|
|
config = fs.readJSONSync(configFilePath + '.json');
|
2018-05-11 22:10:48 +00:00
|
|
|
}
|
2018-01-20 03:08:39 +00:00
|
|
|
let configObject = utils.recursiveMerge(defaultConfig, config);
|
|
|
|
|
|
|
|
if (env) {
|
|
|
|
return utils.recursiveMerge(configObject['default'] || {}, configObject[env]);
|
2017-03-01 02:26:15 +00:00
|
|
|
}
|
2018-08-30 13:53:04 +00:00
|
|
|
return configObject;
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
2018-04-01 01:06:00 +00:00
|
|
|
Config.prototype._getFileOrOject = function(object, filePath, property) {
|
2018-04-01 01:15:53 +00:00
|
|
|
if (typeof (this.configDir) === 'object') {
|
2018-04-01 01:06:00 +00:00
|
|
|
return this.configDir[property];
|
|
|
|
}
|
2018-08-17 17:41:07 +00:00
|
|
|
return utils.joinPath(this.configDir, filePath);
|
2018-04-01 01:06:00 +00:00
|
|
|
};
|
|
|
|
|
2018-01-20 03:08:39 +00:00
|
|
|
Config.prototype.loadBlockchainConfigFile = function() {
|
|
|
|
var configObject = {
|
|
|
|
"default": {
|
2018-04-30 05:56:43 +00:00
|
|
|
"enabled": true,
|
|
|
|
"rpcCorsDomain": "auto",
|
|
|
|
"wsOrigins": "auto"
|
2018-01-20 03:08:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-11 22:10:48 +00:00
|
|
|
let configFilePath = this._getFileOrOject(this.configDir, 'blockchain', 'blockchain');
|
2018-04-02 18:44:55 +00:00
|
|
|
|
|
|
|
this.blockchainConfig = this._mergeConfig(configFilePath, configObject, this.env, true);
|
2018-07-09 19:20:47 +00:00
|
|
|
if (!configFilePath) {
|
|
|
|
this.blockchainConfig.default = true;
|
|
|
|
}
|
2018-09-25 09:27:41 +00:00
|
|
|
|
|
|
|
if (this.blockchainConfig.targetGasLimit && this.blockchainConfig.targetGasLimit.match(unitRegex)) {
|
|
|
|
this.blockchainConfig.targetGasLimit = utils.getWeiBalanceFromString(this.blockchainConfig.targetGasLimit, web3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.blockchainConfig.gasPrice && this.blockchainConfig.gasPrice.match(unitRegex)) {
|
|
|
|
this.blockchainConfig.gasPrice = utils.getWeiBalanceFromString(this.blockchainConfig.gasPrice, web3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.blockchainConfig.account && this.blockchainConfig.account.balance && this.blockchainConfig.account.balance.match(unitRegex)) {
|
|
|
|
this.blockchainConfig.account.balance = utils.getWeiBalanceFromString(this.blockchainConfig.account.balance, web3);
|
|
|
|
}
|
|
|
|
|
2018-09-27 10:57:42 +00:00
|
|
|
if (
|
|
|
|
!this.shownNoAccountConfigMsg &&
|
2018-09-25 09:27:41 +00:00
|
|
|
(/rinkeby|testnet|livenet/).test(this.blockchainConfig.networkType) &&
|
|
|
|
!(this.blockchainConfig.account && this.blockchainConfig.account.address && this.blockchainConfig.account.password) &&
|
2018-09-27 10:57:42 +00:00
|
|
|
!this.blockchainConfig.isDev &&
|
2018-09-05 16:35:45 +00:00
|
|
|
this.env !== 'development' && this.env !== 'test') {
|
2018-09-27 10:57:42 +00:00
|
|
|
this.logger.warn((
|
|
|
|
'\n=== ' + __('Cannot unlock account - account config missing').bold + ' ===\n' +
|
2018-09-25 09:27:41 +00:00
|
|
|
__('Geth is configured to sync to a testnet/livenet and needs to unlock an account ' +
|
2018-09-27 10:57:42 +00:00
|
|
|
'to allow your dApp to interact with geth, however, the address and password must ' +
|
|
|
|
'be specified in your blockchain config. Please update your blockchain config with ' +
|
|
|
|
'a valid address and password: \n') +
|
|
|
|
` - config/blockchain.js > ${this.env} > account\n\n`.italic +
|
|
|
|
__('Please also make sure the keystore file for the account is located at: ') +
|
|
|
|
'\n - Mac: ' + `~/Library/Ethereum/${this.env}/keystore`.italic +
|
|
|
|
'\n - Linux: ' + `~/.ethereum/${this.env}/keystore`.italic +
|
2018-10-04 12:27:57 +00:00
|
|
|
'\n - Windows: ' + `%APPDATA%\\Ethereum\\${this.env}\\keystore`.italic) +
|
2018-09-27 10:57:42 +00:00
|
|
|
__('\n\nAlternatively, you could change ' +
|
2018-09-25 09:27:41 +00:00
|
|
|
`config/blockchain.js > ${this.env} > networkType`.italic +
|
2018-09-27 10:57:42 +00:00
|
|
|
__(' to ') +
|
|
|
|
'"custom"\n'.italic).yellow
|
|
|
|
);
|
|
|
|
this.shownNoAccountConfigMsg = true;
|
|
|
|
}
|
2018-01-20 03:08:39 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadContractsConfigFile = function() {
|
2018-04-01 01:06:00 +00:00
|
|
|
var defaultVersions = {
|
2018-04-20 23:45:57 +00:00
|
|
|
"web3": "1.0.0-beta",
|
2018-09-28 20:32:37 +00:00
|
|
|
"solc": "0.4.25"
|
2018-04-01 01:15:53 +00:00
|
|
|
};
|
2018-04-01 01:06:00 +00:00
|
|
|
var versions = utils.recursiveMerge(defaultVersions, this.embarkConfig.versions || {});
|
|
|
|
|
2017-07-06 00:24:28 +00:00
|
|
|
var configObject = {
|
2017-12-16 13:12:38 +00:00
|
|
|
"default": {
|
2018-04-01 01:06:00 +00:00
|
|
|
"versions": versions,
|
2017-12-16 13:12:38 +00:00
|
|
|
"deployment": {
|
2018-01-20 03:08:39 +00:00
|
|
|
"host": "localhost", "port": 8545, "type": "rpc"
|
2017-12-16 13:12:38 +00:00
|
|
|
},
|
|
|
|
"dappConnection": [
|
|
|
|
"$WEB3",
|
|
|
|
"localhost:8545"
|
|
|
|
],
|
|
|
|
"gas": "auto",
|
|
|
|
"contracts": {
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 00:24:28 +00:00
|
|
|
};
|
2017-03-31 11:39:33 +00:00
|
|
|
|
2017-12-29 13:08:04 +00:00
|
|
|
var contractsConfigs = this.plugins.getPluginsProperty('contractsConfig', 'contractsConfigs');
|
|
|
|
contractsConfigs.forEach(function(pluginConfig) {
|
|
|
|
configObject = utils.recursiveMerge(configObject, pluginConfig);
|
|
|
|
});
|
2017-01-26 11:34:00 +00:00
|
|
|
|
2018-05-11 22:10:48 +00:00
|
|
|
let configFilePath = this._getFileOrOject(this.configDir, 'contracts', 'contracts');
|
2018-09-25 09:27:41 +00:00
|
|
|
let newContractsConfig = this._mergeConfig(configFilePath, configObject, this.env);
|
|
|
|
if (newContractsConfig.gas.match(unitRegex)) {
|
|
|
|
newContractsConfig.gas = utils.getWeiBalanceFromString(newContractsConfig.gas, web3);
|
|
|
|
}
|
|
|
|
if (newContractsConfig.deployment && 'accounts' in newContractsConfig.deployment) {
|
|
|
|
newContractsConfig.deployment.accounts.forEach((account) => {
|
|
|
|
if (account.balance.match(unitRegex)) {
|
|
|
|
account.balance = utils.getWeiBalanceFromString(account.balance, web3);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Object.keys(newContractsConfig.contracts).forEach(contractName => {
|
|
|
|
let gas = newContractsConfig.contracts[contractName].gas;
|
|
|
|
let gasPrice = newContractsConfig.contracts[contractName].gasPrice;
|
|
|
|
if (gas && gas.match(unitRegex)) {
|
|
|
|
newContractsConfig.contracts[contractName].gas = utils.getWeiBalanceFromString(gas, web3);
|
|
|
|
}
|
|
|
|
if (gasPrice && gasPrice.match(unitRegex)) {
|
|
|
|
newContractsConfig.contracts[contractName].gasPrice = utils.getWeiBalanceFromString(gasPrice, web3);
|
|
|
|
}
|
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
if (!deepEqual(newContractsConfig, this.contractsConfig)) {
|
|
|
|
this.contractsConfig = newContractsConfig;
|
|
|
|
}
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
2018-04-18 16:09:42 +00:00
|
|
|
Config.prototype.loadExternalContractsFiles = function() {
|
2018-04-12 21:54:08 +00:00
|
|
|
let contracts = this.contractsConfig.contracts;
|
|
|
|
for (let contractName in contracts) {
|
|
|
|
let contract = contracts[contractName];
|
2018-04-12 22:50:47 +00:00
|
|
|
if (!contract.file) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-17 19:07:00 +00:00
|
|
|
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
|
2018-04-20 13:52:13 +00:00
|
|
|
const fileObj = utils.getExternalContractUrl(contract.file);
|
2018-04-19 14:05:11 +00:00
|
|
|
if (!fileObj) {
|
2018-05-08 21:49:46 +00:00
|
|
|
return this.logger.error(__("HTTP contract file not found") + ": " + contract.file);
|
2018-04-19 14:05:11 +00:00
|
|
|
}
|
2018-04-20 13:52:13 +00:00
|
|
|
const localFile = fileObj.filePath;
|
2018-04-19 14:05:11 +00:00
|
|
|
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: fileObj.url}));
|
2018-04-17 19:07:00 +00:00
|
|
|
} else if (fs.existsSync(contract.file)) {
|
2018-04-18 19:02:50 +00:00
|
|
|
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file}));
|
2018-04-12 22:50:47 +00:00
|
|
|
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
|
2018-04-18 19:02:50 +00:00
|
|
|
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)}));
|
2018-04-13 00:30:20 +00:00
|
|
|
} else {
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.error(__("contract file not found") + ": " + contract.file);
|
2018-04-12 21:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadStorageConfigFile = function() {
|
2018-06-27 00:45:46 +00:00
|
|
|
var versions = utils.recursiveMerge({"ipfs-api": "17.2.4"}, this.embarkConfig.versions || {});
|
2018-04-01 01:06:00 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
var configObject = {
|
2017-03-06 02:27:33 +00:00
|
|
|
"default": {
|
2018-04-01 01:06:00 +00:00
|
|
|
"versions": versions,
|
2017-03-06 02:27:33 +00:00
|
|
|
"enabled": true,
|
2018-04-30 05:56:43 +00:00
|
|
|
"available_providers": ["ipfs", "swarm"],
|
2017-03-06 02:27:33 +00:00
|
|
|
"ipfs_bin": "ipfs",
|
2018-05-25 07:13:57 +00:00
|
|
|
"upload": {
|
|
|
|
"provider": "ipfs",
|
|
|
|
"protocol": "http",
|
2018-07-15 20:33:48 +00:00
|
|
|
"host" : defaultHost,
|
2018-05-25 07:13:57 +00:00
|
|
|
"port": 5001,
|
|
|
|
"getUrl": "http://localhost:8080/ipfs/"
|
2018-05-31 03:19:25 +00:00
|
|
|
},
|
|
|
|
"dappConnection": [{"provider": "ipfs", "host": "localhost", "port": 5001, "getUrl": "http://localhost:8080/ipfs/"}]
|
2017-03-31 11:39:33 +00:00
|
|
|
}
|
2017-03-06 02:27:33 +00:00
|
|
|
};
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2018-05-11 22:10:48 +00:00
|
|
|
let configFilePath = this._getFileOrOject(this.configDir, 'storage', 'storage');
|
2018-04-01 01:06:00 +00:00
|
|
|
|
|
|
|
this.storageConfig = this._mergeConfig(configFilePath, configObject, this.env);
|
2017-02-10 12:44:06 +00:00
|
|
|
};
|
|
|
|
|
2018-05-25 17:25:02 +00:00
|
|
|
Config.prototype.loadNameSystemConfigFile = function() {
|
|
|
|
// todo: spec out names for registration in the file itself for a dev chain
|
|
|
|
var configObject = {
|
|
|
|
"default": {
|
2018-09-12 13:01:21 +00:00
|
|
|
"enabled": false
|
2018-05-25 17:25:02 +00:00
|
|
|
}
|
2018-05-25 17:52:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let configFilePath = this._getFileOrOject(this.configDir, 'namesystem', 'namesystem');
|
|
|
|
|
|
|
|
this.namesystemConfig = this._mergeConfig(configFilePath, configObject, this.env);
|
2017-02-10 12:44:06 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadCommunicationConfigFile = function() {
|
|
|
|
var configObject = {
|
2017-03-06 02:27:33 +00:00
|
|
|
"default": {
|
|
|
|
"enabled": true,
|
|
|
|
"provider": "whisper",
|
2018-06-01 14:15:41 +00:00
|
|
|
"available_providers": ["whisper"],
|
2017-10-19 22:55:49 +00:00
|
|
|
"connection": {
|
2018-07-15 20:33:48 +00:00
|
|
|
"host": defaultHost,
|
|
|
|
"port": 8546,
|
|
|
|
"type": "ws"
|
2017-10-19 22:55:49 +00:00
|
|
|
}
|
2017-03-06 02:27:33 +00:00
|
|
|
}
|
|
|
|
};
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2018-05-11 22:10:48 +00:00
|
|
|
let configFilePath = this._getFileOrOject(this.configDir, 'communication', 'communication');
|
2018-04-01 01:06:00 +00:00
|
|
|
|
|
|
|
this.communicationConfig = this._mergeConfig(configFilePath, configObject, this.env);
|
2017-02-10 12:44:06 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadWebServerConfigFile = function() {
|
2018-01-20 03:08:39 +00:00
|
|
|
var configObject = {
|
2018-07-15 20:33:48 +00:00
|
|
|
"enabled": true,
|
|
|
|
"host": defaultHost,
|
2018-09-17 22:53:27 +00:00
|
|
|
"openBrowser": true,
|
2018-07-15 20:33:48 +00:00
|
|
|
"port": 8000
|
2017-03-04 19:20:28 +00:00
|
|
|
};
|
2018-01-20 03:08:39 +00:00
|
|
|
|
2018-05-11 22:10:48 +00:00
|
|
|
let configFilePath = this._getFileOrOject(this.configDir, 'webserver', 'webserver');
|
2018-04-01 01:06:00 +00:00
|
|
|
|
2018-07-16 04:49:24 +00:00
|
|
|
let webServerConfig = this._mergeConfig(configFilePath, configObject, false);
|
|
|
|
|
2018-09-04 20:44:42 +00:00
|
|
|
if (configFilePath === false) {
|
|
|
|
this.webServerConfig = {enabled: false};
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 04:49:24 +00:00
|
|
|
if (this.webServerConfig) {
|
2018-09-07 18:54:15 +00:00
|
|
|
// cli flags to `embark run` should override configFile and defaults (configObject)
|
2018-07-16 04:49:24 +00:00
|
|
|
this.webServerConfig = utils.recursiveMerge(webServerConfig, this.webServerConfig);
|
|
|
|
} else {
|
|
|
|
this.webServerConfig = webServerConfig;
|
|
|
|
}
|
2017-03-04 19:20:28 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadEmbarkConfigFile = function() {
|
2018-08-20 13:27:23 +00:00
|
|
|
var configObject = {
|
|
|
|
options: {
|
|
|
|
solc: {
|
|
|
|
"optimize": true,
|
|
|
|
"optimize-runs": 200
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-20 18:03:10 +00:00
|
|
|
this.embarkConfig = utils.recursiveMerge(configObject, this.embarkConfig);
|
2018-08-20 13:27:23 +00:00
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
const contracts = this.embarkConfig.contracts;
|
|
|
|
const newContractsFiles = this.loadFiles(contracts);
|
|
|
|
if (!this.contractFiles || newContractsFiles.length !== this.contractFiles.length || !deepEqual(newContractsFiles, this.contractFiles)) {
|
2018-08-08 21:10:36 +00:00
|
|
|
this.contractsFiles = this.contractsFiles.concat(newContractsFiles).filter((file, index, arr) => {
|
|
|
|
return !arr.some((file2, index2) => {
|
|
|
|
return file.filename === file2.filename && index < index2;
|
|
|
|
});
|
|
|
|
});
|
2018-04-30 05:56:43 +00:00
|
|
|
}
|
2017-12-15 22:14:00 +00:00
|
|
|
// determine contract 'root' directories
|
|
|
|
this.contractDirectories = contracts.map((dir) => {
|
|
|
|
return dir.split("**")[0];
|
|
|
|
}).map((dir) => {
|
|
|
|
return dir.split("*.")[0];
|
|
|
|
});
|
2018-04-19 18:26:11 +00:00
|
|
|
this.contractDirectories.push(constants.httpContractsDirectory);
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
this.buildDir = this.embarkConfig.buildDir;
|
2017-03-01 02:26:15 +00:00
|
|
|
this.configDir = this.embarkConfig.config;
|
|
|
|
};
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadPipelineConfigFile = function() {
|
2018-10-04 09:40:52 +00:00
|
|
|
|
|
|
|
const defaultPipelineConfig = {
|
|
|
|
typescript: false
|
|
|
|
};
|
|
|
|
|
|
|
|
let pipelineConfigPath = this._getFileOrOject(this.configDir, 'pipeline', 'pipeline');
|
|
|
|
|
|
|
|
// Embark applications in "simple" mode that aren't aware of `pipeline.js` configuration capabilities
|
|
|
|
// won't have a pipeline config path so we need to perform this safety check here, otherwise the
|
|
|
|
// next expression is going to throw.
|
|
|
|
if (pipelineConfigPath !== undefined) {
|
|
|
|
// At this point, `pipelineConfigPath` could be either `config/pipeline` or a filepath including its extension.
|
|
|
|
// We need to make sure that we always have an extension.
|
|
|
|
pipelineConfigPath = `${fs.dappPath(pipelineConfigPath)}${path.extname(pipelineConfigPath) === '.js' ? '' : '.js'}`;
|
2016-08-22 03:40:05 +00:00
|
|
|
}
|
2018-10-04 09:40:52 +00:00
|
|
|
|
|
|
|
let pipelineConfig = defaultPipelineConfig;
|
|
|
|
|
|
|
|
if (pipelineConfigPath && fs.existsSync(pipelineConfigPath)) {
|
|
|
|
delete require.cache[pipelineConfigPath];
|
|
|
|
pipelineConfig = utils.recursiveMerge(
|
|
|
|
utils.recursiveMerge(true, pipelineConfig),
|
|
|
|
require(pipelineConfigPath)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pipelineConfig = pipelineConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.loadAssetFiles = function () {
|
|
|
|
Object.keys(this.embarkConfig.app).forEach(targetFile => {
|
|
|
|
this.assetFiles[targetFile] = this.loadFiles(this.embarkConfig.app[targetFile]);
|
|
|
|
});
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
2016-09-25 01:10:47 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadChainTrackerFile = function() {
|
2018-01-20 03:08:39 +00:00
|
|
|
if (!fs.existsSync(this.chainsFile)) {
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.info(this.chainsFile + ' ' + __('file not found, creating it...'));
|
2017-02-19 03:40:42 +00:00
|
|
|
fs.writeJSONSync(this.chainsFile, {});
|
2016-09-25 01:10:47 +00:00
|
|
|
}
|
2018-01-20 03:08:39 +00:00
|
|
|
|
|
|
|
this.chainTracker = fs.readJSONSync(this.chainsFile);
|
2016-09-25 01:10:47 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-02-24 01:36:11 +00:00
|
|
|
function findMatchingExpression(filename, filesExpressions) {
|
|
|
|
for (let fileExpression of filesExpressions) {
|
|
|
|
var matchingFiles = utils.filesMatchingPattern(fileExpression);
|
2018-02-24 14:26:43 +00:00
|
|
|
for (let matchFile of matchingFiles) {
|
2018-02-24 01:36:11 +00:00
|
|
|
if (matchFile === filename) {
|
2018-02-24 14:26:43 +00:00
|
|
|
return path.dirname(fileExpression).replace(/\*/g, '');
|
2018-02-24 01:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-24 14:26:43 +00:00
|
|
|
return path.dirname(filename);
|
2018-02-24 01:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadFiles = function(files) {
|
|
|
|
var self = this;
|
|
|
|
var originalFiles = utils.filesMatchingPattern(files);
|
|
|
|
var readFiles = [];
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
originalFiles.filter(function(file) {
|
2017-07-03 22:54:31 +00:00
|
|
|
return (file[0] === '$' || file.indexOf('.') >= 0);
|
2017-03-31 11:39:33 +00:00
|
|
|
}).filter(function(file) {
|
2018-02-24 01:36:11 +00:00
|
|
|
let basedir = findMatchingExpression(file, files);
|
2018-04-18 19:02:50 +00:00
|
|
|
readFiles.push(new File({filename: file, type: File.types.dapp_file, basedir: basedir, path: file}));
|
2016-08-22 03:40:05 +00:00
|
|
|
});
|
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
var filesFromPlugins = [];
|
|
|
|
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
|
2018-01-20 03:08:39 +00:00
|
|
|
filePlugins.forEach(function(plugin) {
|
|
|
|
try {
|
|
|
|
var fileObjects = plugin.runFilePipeline();
|
|
|
|
for (var i=0; i < fileObjects.length; i++) {
|
|
|
|
var fileObject = fileObjects[i];
|
|
|
|
filesFromPlugins.push(fileObject);
|
2017-02-03 11:30:08 +00:00
|
|
|
}
|
2018-01-20 03:08:39 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
}
|
|
|
|
});
|
2017-03-31 11:39:33 +00:00
|
|
|
filesFromPlugins.filter(function(file) {
|
2018-04-30 05:56:43 +00:00
|
|
|
if ((file.intendedPath && utils.fileMatchesPattern(files, file.intendedPath)) || utils.fileMatchesPattern(files, file.file)) {
|
2017-02-03 11:30:08 +00:00
|
|
|
readFiles.push(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-22 03:40:05 +00:00
|
|
|
return readFiles;
|
|
|
|
};
|
|
|
|
|
2018-06-18 15:25:43 +00:00
|
|
|
// NOTE: this doesn't work for internal modules
|
2017-03-31 11:39:33 +00:00
|
|
|
Config.prototype.loadPluginContractFiles = function() {
|
|
|
|
var self = this;
|
2017-01-26 11:34:00 +00:00
|
|
|
|
2017-03-31 11:39:33 +00:00
|
|
|
var contractsPlugins = this.plugins.getPluginsFor('contractFiles');
|
2018-01-20 03:08:39 +00:00
|
|
|
contractsPlugins.forEach(function(plugin) {
|
|
|
|
plugin.contractsFiles.forEach(function(file) {
|
|
|
|
var filename = file.replace('./','');
|
2018-10-02 10:32:54 +00:00
|
|
|
self.contractsFiles.push(new File({filename: filename, pluginPath: plugin.pluginPath, type: File.types.custom, path: filename, resolver: function(callback) {
|
2018-01-20 03:08:39 +00:00
|
|
|
callback(plugin.loadPluginFile(file));
|
|
|
|
}}));
|
2017-01-26 11:34:00 +00:00
|
|
|
});
|
2018-01-20 03:08:39 +00:00
|
|
|
});
|
2017-01-26 11:34:00 +00:00
|
|
|
};
|
|
|
|
|
2016-08-22 03:40:05 +00:00
|
|
|
module.exports = Config;
|