diff --git a/lib/core/config.js b/lib/core/config.js index c3932c46a..cebc9282e 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -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; diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index ac3465111..cb2bd5024 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -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(); diff --git a/lib/pipeline/watch.js b/lib/pipeline/watch.js index 585ed7785..703273142 100644 --- a/lib/pipeline/watch.js +++ b/lib/pipeline/watch.js @@ -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);