Merge pull request #795 from embark-framework/bug_fix/webserver-config-change

Watch webserver config and restart it
This commit is contained in:
Iuri Matias 2018-09-07 19:04:17 -04:00 committed by GitHub
commit 833fcef22a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 9 deletions

View File

@ -328,7 +328,7 @@ Config.prototype.loadWebServerConfigFile = function() {
return;
}
if (this.webServerConfig) {
// cli falgs to `embark run` should override configFile and defaults (configObject)
// cli flags to `embark run` should override configFile and defaults (configObject)
this.webServerConfig = utils.recursiveMerge(webServerConfig, this.webServerConfig);
} else {
this.webServerConfig = webServerConfig;

View File

@ -32,6 +32,26 @@ class WebServer {
port: this.port
});
this.events.on('webserver:config:change', () => {
this.embark.config.webServerConfig = null;
this.embark.config.loadWebServerConfigFile();
this.webServerConfig = this.embark.config.webServerConfig;
this.host = this.webServerConfig.host;
this.port = this.webServerConfig.port;
this.server.host = this.host;
this.server.port = this.port;
this.testPort(() => {
this.server.stop((_err) => {
this.server.start((_err, message, port) => {
this.logger.info(message);
this.port = port;
this.events.request('open-browser', () => {});
});
});
});
});
this.testPort(() => {
this.listenToCommands();
this.registerConsoleCommands();

View File

@ -26,8 +26,12 @@ class Watch {
self.logger.trace('ready to watch contract changes');
});
this.watchConfigs(embarkConfig, function () {
self.logger.trace('ready to watch config changes');
this.watchContractConfig(embarkConfig, function () {
self.logger.trace('ready to watch contract config changes');
});
this.watchWebserverConfig(embarkConfig, function () {
self.logger.trace('ready to watch webserver config changes');
});
this.logger.info(__("ready to watch file changes"));
@ -96,19 +100,45 @@ class Watch {
);
}
watchConfigs(embarkConfig, callback) {
watchWebserverConfig(embarkConfig, callback) {
let self = this;
let contractsConfig;
if (typeof embarkConfig.config === 'object' || embarkConfig.config.contracts) {
contractsConfig = embarkConfig.config.contracts;
let webserverConfig;
if (typeof embarkConfig.config === 'object') {
if (!embarkConfig.config.webserver) {
return;
}
webserverConfig = embarkConfig.config.webserver;
} else {
let contractsFolder = embarkConfig.config.replace(/\\/g, '/');
if (contractsFolder.charAt(contractsFolder.length - 1) !== '/') {
contractsFolder += '/';
}
contractsConfig = [`${contractsFolder}**/contracts.json`, `${contractsFolder}**/contracts.js`];
webserverConfig = [`${contractsFolder}**/webserver.json`, `${contractsFolder}**/webserver.js`];
}
this.watchFiles(contractsConfig,
this.watchFiles(webserverConfig,
function (eventName, path) {
self.logger.info(`${eventName}: ${path}`);
self.events.emit('webserver:config:change', 'config', path);
},
function () {
callback();
}
);
}
watchContractConfig(embarkConfig, callback) {
let self = this;
let contractConfig;
if (typeof embarkConfig.config === 'object' || embarkConfig.config.contracts) {
contractConfig = embarkConfig.config.contracts;
} else {
let contractsFolder = embarkConfig.config.replace(/\\/g, '/');
if (contractsFolder.charAt(contractsFolder.length - 1) !== '/') {
contractsFolder += '/';
}
contractConfig = [`${contractsFolder}**/contracts.json`, `${contractsFolder}**/contracts.js`];
}
this.watchFiles(contractConfig,
function (eventName, path) {
self.logger.info(`${eventName}: ${path}`);
self.events.emit('file-' + eventName, 'config', path);