diff --git a/lib/core/engine.js b/lib/core/engine.js index fe346a00f..393c16018 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -210,11 +210,9 @@ class Engine { }); } - fileWatchService(_options) { - this.events.emit("status", "Watching for changes"); - const Watch = require('../pipeline/watch.js'); - this.watch = new Watch({logger: this.logger, events: this.events}); - this.watch.start(); + fileWatchService() { + this.registerModule('watcher'); + this.events.request('watcher:start'); } webServerService() { diff --git a/lib/modules/pipeline/webpackProcess.js b/lib/modules/pipeline/webpackProcess.js index 73051261f..4303ebb42 100644 --- a/lib/modules/pipeline/webpackProcess.js +++ b/lib/modules/pipeline/webpackProcess.js @@ -3,7 +3,7 @@ const fs = require('../../core/fs'); const ProcessWrapper = require('../../core/processes/processWrapper'); const webpack = require('webpack'); const writeFile = require('util').promisify(require('fs').writeFile); -const {errorMessage, recursiveMerge} = require('../utils/utils'); +const {errorMessage, recursiveMerge} = require('../../utils/utils'); // TODO: refactor into lib/core/config once pipeline is a proper embark module const defaultPipelineConfig = { diff --git a/lib/pipeline/watch.js b/lib/modules/watcher/index.js similarity index 77% rename from lib/pipeline/watch.js rename to lib/modules/watcher/index.js index 703273142..26dc1ee4f 100644 --- a/lib/pipeline/watch.js +++ b/lib/modules/watcher/index.js @@ -1,15 +1,23 @@ let chokidar = require('chokidar'); let path = require('path'); -let fs = require('../core/fs.js'); +let fs = require('../../core/fs.js'); + +const DAPP_PIPELINE_CONFIG_FILE = 'pipeline.js'; +const DAPP_WEBPACK_CONFIG_FILE = 'webpack.config.js'; +const DAPP_BABEL_LOADER_OVERRIDES_CONFIG_FILE = 'babel-loader-overrides.js'; // TODO: this should be receiving the config object not re-reading the // embark.json file -class Watch { - constructor(options) { - this.logger = options.logger; - this.events = options.events; +class Watcher { + constructor(embark) { + this.logger = embark.logger; + this.events = embark.events; this.fileWatchers = []; + + this.events.setCommandHandler('watcher:start', () => this.start()); + this.events.setCommandHandler('watcher:stop', () => this.stop()); + this.events.setCommandHandler('watcher:restart', () => this.restart()); } start() { @@ -30,6 +38,10 @@ class Watch { self.logger.trace('ready to watch contract config changes'); }); + this.watchPipelineConfig(embarkConfig, function () { + self.logger.trace('ready to watch pipeline config changes'); + }); + this.watchWebserverConfig(embarkConfig, function () { self.logger.trace('ready to watch webserver config changes'); }); @@ -150,6 +162,25 @@ class Watch { ); } + watchPipelineConfig(embarkConfig, callback) { + let filesToWatch = [ + fs.dappPath('', DAPP_WEBPACK_CONFIG_FILE), + fs.dappPath('', DAPP_BABEL_LOADER_OVERRIDES_CONFIG_FILE) + ]; + + if (typeof embarkConfig.config === 'object' && embarkConfig.config.pipeline) { + filesToWatch.push(embarkConfig.config.pipeline); + } else if (typeof embarkConfig.config === 'string') { + filesToWatch.push(fs.dappPath(embarkConfig.config, DAPP_PIPELINE_CONFIG_FILE)); + } + + this.watchFiles(filesToWatch, (eventName, path) => { + this.logger.info(`${eventName}: ${path}`); + this.events.emit('file-' + eventName, 'config', path); + this.events.emit('file-event', 'config', path); + }, callback); + } + watchFiles(files, changeCallback, doneCallback) { this.logger.trace('watchFiles'); this.logger.trace(files); @@ -172,4 +203,5 @@ class Watch { } -module.exports = Watch; +module.exports = Watcher; +