UNPKG

kist

Version:

Lightweight Package Pipeline Processor with Plugin Architecture

74 lines 2.53 kB
import { __awaiter } from "tslib"; import chokidar from "chokidar"; import { AbstractProcess } from "../core/abstract/AbstractProcess.js"; import { ConfigStore } from "../core/config/ConfigStore.js"; export class LiveWatcher extends AbstractProcess { constructor(onChange) { var _a, _b; super(); this.watcher = null; const liveReloadOptions = ConfigStore.getInstance().get("options.live") || {}; this.pathsToWatch = (_a = liveReloadOptions.watchPaths) !== null && _a !== void 0 ? _a : [ "src/**/*", "config/**/*", "pack.yaml", ]; this.ignoredPaths = (_b = liveReloadOptions.ignoredPaths) !== null && _b !== void 0 ? _b : ["node_modules"]; this.onChange = onChange; this.startWatching(); } setupWatchers() { if (!this.watcher) return; this.watcher .on("ready", () => { this.logInfo("File watching is active. Waiting for changes..."); }) .on("change", (filePath) => { this.logInfo(`File changed: ${filePath}`); try { this.onChange(filePath); } catch (error) { this.logError(`Error handling file change for ${filePath}:`, error); } }) .on("error", (error) => { this.logError("Watcher encountered an error:", error); }); } startWatching() { if (this.watcher) { this.logInfo("Watcher is already running."); return; } this.logInfo("Starting file watcher..."); this.watcher = chokidar.watch(this.pathsToWatch, { ignored: this.ignoredPaths, persistent: true, ignoreInitial: true, awaitWriteFinish: { pollInterval: 100, stabilityThreshold: 100, }, }); this.setupWatchers(); } stopWatching() { return __awaiter(this, void 0, void 0, function* () { if (this.watcher) { yield this.watcher.close(); this.logInfo("File watching has been stopped."); this.watcher = null; } }); } restartWatcher() { return __awaiter(this, void 0, void 0, function* () { this.logInfo("Restarting file watcher..."); yield this.stopWatching(); this.startWatching(); }); } } //# sourceMappingURL=LiveWatcher.js.map