UNPKG

@roots/bud-server

Version:

Development server for @roots/bud

80 lines (79 loc) 1.9 kB
import { __decorate } from "tslib"; import * as chokidar from '@roots/bud-support/chokidar'; import { bind } from '@roots/bud-support/decorators/bind'; import logger from '@roots/bud-support/logger'; /** * FS Watcher */ export class Watcher { _app; /** * Watch files */ files; /** * Watcher instance */ instance; /** * Watch options */ options; /** * Class constructor * * @param app - Application instance */ constructor(_app) { this._app = _app; } /** * App instance */ get app() { return this._app(); } /** * Logger */ get logger() { return logger.scope(`watcher`); } /** * Initialize watch files */ async watch() { if (this.app.context.dry) return; this.files = this.app.hooks.filter(`dev.watch.files`, new Set([])); this.options = this.app.hooks.filter(`dev.watch.options`, { cwd: this.app.path(), ignoreInitial: true, }); if (this.files.size < 1) return; this.instance = chokidar .watch([...this.files], this.options) .on(`change`, this.watcherCallback) .on(`add`, this.watcherCallback) .on(`unlink`, this.watcherCallback); this.logger.log(`watching`); return this.instance; } /** * Watcher callback */ watcherCallback(path) { this.logger.log(`edit to`, path.replace(this.app.path(), `.`), `triggered reload`); this.app.server?.appliedMiddleware?.hot?.publish({ action: `reload`, message: `Detected file change: ${path}. Reloading window.`, }); } } __decorate([ bind ], Watcher.prototype, "watch", null); __decorate([ bind ], Watcher.prototype, "watcherCallback", null);