Enable LLMs to handle webhooks with plaintext files
1import chokidar from "chokidar";
2import type { FSWatcher } from "chokidar";
3import * as nodePath from "node:path";
4import type { LureCache } from "./loader.js";
5
6export function watchLures(luresDir: string, cache: LureCache): FSWatcher {
7 const pattern = nodePath.join(nodePath.resolve(luresDir), "**", "*.lure").replace(/\\/g, "/");
8
9 const watcher = chokidar.watch(pattern, { ignoreInitial: true });
10
11 watcher
12 .on("add", (filePath: string) => {
13 void (async () => {
14 try {
15 await cache.set(filePath);
16 } catch (error) {
17 console.error(`Failed to load new lure ${filePath}:`, error);
18 }
19 })();
20 })
21 .on("change", (filePath: string) => {
22 void (async () => {
23 try {
24 await cache.set(filePath);
25 } catch (error) {
26 console.error(`Failed to reload lure ${filePath}:`, error);
27 }
28 })();
29 })
30 .on("unlink", (filePath: string) => {
31 cache.delete(filePath);
32 });
33
34 return watcher;
35}