kist
Version:
Lightweight Package Pipeline Processor with Plugin Architecture
71 lines • 2.28 kB
JavaScript
import chokidar from "chokidar";
import { AbstractProcess } from "../core/abstract/AbstractProcess.js";
import { ConfigStore } from "../core/config/ConfigStore.js";
export class LiveWatcher extends AbstractProcess {
watcher = null;
pathsToWatch;
ignoredPaths;
onChange;
constructor(onChange) {
super();
const liveReloadOptions = ConfigStore.getInstance().get("options.live") || {};
this.pathsToWatch = liveReloadOptions.watchPaths ?? [
"src/**/*",
"config/**/*",
"pack.yaml",
];
this.ignoredPaths = liveReloadOptions.ignoredPaths ?? ["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();
}
async stopWatching() {
if (this.watcher) {
await this.watcher.close();
this.logInfo("File watching has been stopped.");
this.watcher = null;
}
}
async restartWatcher() {
this.logInfo("Restarting file watcher...");
await this.stopWatching();
this.startWatching();
}
}
//# sourceMappingURL=LiveWatcher.js.map