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
This commit is contained in:
Pascal Precht 2018-10-03 17:15:16 +02:00
parent 081b7eee89
commit 71e92358a8
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 42 additions and 12 deletions

View File

@ -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() {

View File

@ -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 = {

View File

@ -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;