From 71e92358a832d881d2def2db9c8817d360b59474 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Wed, 3 Oct 2018 17:15:16 +0200 Subject: [PATCH] feat(modules/watcher): introduce watcher plugin module As part of a bigger refactoring to make Embark's build pipeline pluggable, this commit moves the watcher into its own plugin module so it can be consumed via Embark's event bus. It also introduces new command handlers for all watcher related APIs respectively: - watcher:start - watcher:stop - watcher:restart --- lib/core/engine.js | 8 ++-- lib/modules/pipeline/webpackProcess.js | 2 +- .../watch.js => modules/watcher/index.js} | 44 ++++++++++++++++--- 3 files changed, 42 insertions(+), 12 deletions(-) rename lib/{pipeline/watch.js => modules/watcher/index.js} (77%) diff --git a/lib/core/engine.js b/lib/core/engine.js index fe346a00..393c1601 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 73051261..4303ebb4 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 70327314..26dc1ee4 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; +