mirror of https://github.com/embarklabs/embark.git
feat(blockchain/config): adds a cors command to add it to the config
This commit is contained in:
parent
1bafc41d97
commit
84a74ac4a2
|
@ -15,7 +15,6 @@ import * as utilsContractsConfig from "../utils/contractsConfig";
|
|||
const DEFAULT_CONFIG_PATH = 'config/';
|
||||
|
||||
var Config = function(options) {
|
||||
const self = this;
|
||||
this.env = options.env || 'default';
|
||||
this.blockchainConfig = {};
|
||||
this.contractsConfig = {};
|
||||
|
@ -35,37 +34,75 @@ var Config = function(options) {
|
|||
this.shownNoAccountConfigMsg = false; // flag to ensure "no account config" message is only displayed once to the user
|
||||
this.corsParts = [];
|
||||
this.providerUrl = null;
|
||||
this.events.setCommandHandler("config:cors:add", (url) => {
|
||||
|
||||
this.registerEvents();
|
||||
};
|
||||
|
||||
Config.prototype.registerEvents = function() {
|
||||
this.events.setCommandHandler("config:cors:add", (url, cb = () => {}) => {
|
||||
this.corsParts.push(url);
|
||||
this._updateBlockchainCors();
|
||||
|
||||
let configFilePath = this._getFileOrOject(this.configDir, 'blockchain', 'blockchain');
|
||||
const completeConfig = this._mergeConfig(configFilePath, {}, null, true);
|
||||
const envConfig = completeConfig[this.env];
|
||||
|
||||
if (!envConfig.rpcCorsDomain || envConfig.rpcCorsDomain === 'auto') {
|
||||
envConfig.rpcCorsDomain = {auto: true, additionalCors: []};
|
||||
} else if (typeof envConfig.rpcCorsDomain === 'string') {
|
||||
envConfig.rpcCorsDomain = {auto: false, additionalCors: envConfig.rpcCorsDomain.join(',')};
|
||||
} else if (!envConfig.rpcCorsDomain.additionalCors) {
|
||||
envConfig.rpcCorsDomain.additionalCors = [];
|
||||
}
|
||||
if (!envConfig.wsOrigins || envConfig.wsOrigins === 'auto') {
|
||||
envConfig.wsOrigins = {auto: true, additionalCors: []};
|
||||
} else if (typeof envConfig.wsOrigins === 'string') {
|
||||
envConfig.wsOrigins = {auto: false, additionalCors: envConfig.wsOrigins.join(',')};
|
||||
} else if (!envConfig.wsOrigins.additionalCors) {
|
||||
envConfig.wsOrigins.additionalCors = [];
|
||||
}
|
||||
|
||||
envConfig.rpcCorsDomain.additionalCors.push(url);
|
||||
envConfig.wsOrigins.additionalCors.push(url);
|
||||
|
||||
let configString = JSON.stringify(completeConfig, null, 2);
|
||||
if (fs.existsSync(configFilePath + '.js')) {
|
||||
configFilePath += '.js';
|
||||
// TODO Find a better way, because if the users have more than the config, this doesn't work
|
||||
configString = 'module.exports = ' + configString;
|
||||
} else {
|
||||
configFilePath += '.json';
|
||||
}
|
||||
|
||||
fs.writeFile(configFilePath, configString, cb);
|
||||
this.logger.info(__('Added "%s" to the CORS. You will need to restart your blockchain node or Embark for the changes to take effect', url));
|
||||
});
|
||||
|
||||
|
||||
self.events.setCommandHandler("config:contractsConfig", (cb) => {
|
||||
cb(self.contractsConfig);
|
||||
this.events.setCommandHandler("config:contractsConfig", (cb) => {
|
||||
cb(this.contractsConfig);
|
||||
});
|
||||
|
||||
self.events.setCommandHandler("config:contractsConfig:set", (config, cb) => {
|
||||
self.contractsConfig = config;
|
||||
this.events.setCommandHandler("config:contractsConfig:set", (config, cb) => {
|
||||
this.contractsConfig = config;
|
||||
cb();
|
||||
});
|
||||
|
||||
self.events.setCommandHandler("config:contractsFiles", (cb) => {
|
||||
cb(self.contractsFiles);
|
||||
this.events.setCommandHandler("config:contractsFiles", (cb) => {
|
||||
cb(this.contractsFiles);
|
||||
});
|
||||
|
||||
// TODO: refactor this so reading the file can be done with a normal resolver or something that takes advantage of the plugin api
|
||||
self.events.setCommandHandler("config:contractsFiles:add", (filename, resolver) => {
|
||||
this.events.setCommandHandler("config:contractsFiles:add", (filename, resolver) => {
|
||||
resolver = resolver || function(callback) {
|
||||
callback(fs.readFileSync(filename).toString());
|
||||
};
|
||||
self.contractsFiles.push(new File({filename, type: File.types.custom, path: filename, resolver}));
|
||||
this.contractsFiles.push(new File({filename, type: File.types.custom, path: filename, resolver}));
|
||||
});
|
||||
|
||||
self.events.on('file-remove', (fileType, removedPath) => {
|
||||
this.events.on('file-remove', (fileType, removedPath) => {
|
||||
if(fileType !== 'contract') return;
|
||||
const normalizedPath = path.normalize(removedPath);
|
||||
self.contractsFiles = self.contractsFiles.filter(file => path.normalize(file.filename) !== normalizedPath);
|
||||
this.contractsFiles = this.contractsFiles.filter(file => path.normalize(file.filename) !== normalizedPath);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -17,6 +17,19 @@ class BlockchainModule {
|
|||
this.client = options.client;
|
||||
|
||||
this.registerBlockchainProcess();
|
||||
|
||||
embark.registerConsoleCommand({
|
||||
matches: (cmd) => {
|
||||
const [cmdName] = cmd.split(' ');
|
||||
return cmdName === 'addBlockchainCors' || cmdName === 'addCors';
|
||||
},
|
||||
usage: 'addBlockchainCors/addCors <url>',
|
||||
description: 'Adds a CORS domain/origin to the blockchain configuration',
|
||||
process: (cmd, callback) => {
|
||||
const [_cmdName, url] = cmd.split(' ');
|
||||
this.events.request('config:cors:add', url, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
registerBlockchainProcess() {
|
||||
|
|
Loading…
Reference in New Issue