deno.land / x / lume@v2.1.4 / core / watcher.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { join, relative } from "../deps/path.ts";import { normalizePath } from "./utils/path.ts";import Events from "./events.ts";
import type Site from "./site.ts";import type { Event, EventListener, EventOptions } from "./events.ts";
/** The options to configure the local server */export interface Options { /** The folder root to watch */ root: string;
/** Paths ignored by the watcher */ ignore?: (string | ((path: string) => boolean))[];
/** The debounce waiting time */ debounce?: number;}
/** Custom events for server */export interface WatchEvent extends Event { /** The event type */ type: WatchEventType;
/** The list of changed files (only for "change" events) */ files?: Set<string>;
/** The error object (only for "error" events) */ error?: Error;}
/** The available event types */export type WatchEventType = | "start" | "change" | "error";
export interface Watcher { /** Add a listener to an event */ addEventListener( type: WatchEventType, listener: EventListener<WatchEvent>, options?: EventOptions, ): this;
/** Dispatch an event */ dispatchEvent(event: WatchEvent): Promise<boolean>;
/** Start the watcher */ start(): Promise<void>;}
export default class FSWatcher implements Watcher { events: Events<WatchEvent> = new Events<WatchEvent>(); options: Options;
constructor(options: Options) { this.options = options; }
/** Add a listener to an event */ addEventListener( type: WatchEventType, listener: EventListener<WatchEvent>, options?: EventOptions, ) { this.events.addEventListener(type, listener, options); return this; }
/** Dispatch an event */ dispatchEvent(event: WatchEvent) { return this.events.dispatchEvent(event); }
/** Start the file watcher */ async start() { const { root, ignore, debounce } = this.options; const watcher = Deno.watchFs(root); const changes = new Set<string>(); let timer = 0; let runningCallback = false;
await this.dispatchEvent({ type: "start" });
const callback = async () => { if (!changes.size || runningCallback) { return; }
const files = new Set(changes); changes.clear();
runningCallback = true;
try { const result = await this.dispatchEvent({ type: "change", files }); if (false === result) { return watcher.close(); } } catch (error) { await this.dispatchEvent({ type: "error", error }); } runningCallback = false; };
for await (const event of watcher) { let paths = event.paths.map((path) => normalizePath(path));
// Filter ignored paths paths = paths.filter((path) => ignore ? !ignore.some((ignore) => typeof ignore === "string" ? (path.startsWith(normalizePath(join(root, ignore, "/"))) || path === normalizePath(join(root, ignore))) : ignore(path) ) : true );
if (!paths.length) { continue; }
paths.forEach((path) => changes.add(normalizePath(relative(root, path))));
// Debounce clearTimeout(timer); timer = setTimeout(callback, debounce ?? 100); } }}
export class SiteWatcher implements Watcher { site: Site; events: Events<WatchEvent> = new Events<WatchEvent>();
constructor(site: Site) { this.site = site; }
/** Add a listener to an event */ addEventListener( type: WatchEventType, listener: EventListener<WatchEvent>, options?: EventOptions, ) { this.events.addEventListener(type, listener, options); return this; }
/** Dispatch an event */ dispatchEvent(event: WatchEvent) { return this.events.dispatchEvent(event); }
/** Start the watcher */ async start() { await this.dispatchEvent({ type: "start" }); this.site.addEventListener("afterUpdate", (event) => { const files = new Set([ ...event.pages.map((page) => page.outputPath), ...event.staticFiles.map((file) => file.outputPath), ]); this.dispatchEvent({ type: "change", files }); }); }}
lume

Version Info

Tagged at
7 months ago