-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.ts
186 lines (173 loc) · 6.62 KB
/
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {log, path} from "./deps.ts";
import {reduceVoid, reduceVoidPromise} from "./utils.ts";
import Runner from "./runner.ts";
import Job from "./job.ts";
export type Watch = {
/**
* Cancels this watch, closing underlying system resources and preventing
* new events from being received.
*/
cancel: () => void;
/**
* A promise that returns when the underlying watch is closed. This will
* likely block until cancel is called.
*/
promise: Promise<void>;
}
export default class Watcher {
readonly runner;
/**
* Creates a Watcher. See Watcher#file and Watcher#folder for more details
* on usage.
*
* @param runner The runner to use when changes are detected
*/
constructor(runner: Runner) {
this.runner = runner;
}
/**
* Watch a job. When any files covered by this job are modified, then this
* job will be run against those files.
*
* @param job The job to watch
*/
file(job: Job): Watch {
return job.batches.map(batch => this.watch(
path.join(path.dirname(job.file), batch.watch.folder),
batch.watch.files,
file => {
const promises = [];
if (batch.edits)
promises.push(this.runner.edits(file, batch.edits));
if (batch.chapters)
promises.push(this.runner.chapters(file, batch.chapters));
return Promise
.all(promises)
.then((edits: number[]) => edits.reduce((a, b) => a + b));
})
).reduce((last, current) => {
return {
cancel: reduceVoid(last.cancel, current.cancel),
promise: reduceVoidPromise(last.promise, current.promise)
};
});
}
/**
* Watch a folder for job files. When these files are detected, their jobs
* will be watched also. Note that this will not run the detected jobs,
* but will run them on any files changed after the watch has started.
*
* @param folder The folder (and subfolders) to watch for jobs
*/
folder(folder: string): Watch {
const watching = new Map<string, Watch>();
const dir = path.join(Deno.cwd(), folder)
for (const entry of Deno.readDirSync(dir)) {
if (entry.name.endsWith("automkv.yml")) {
const file = path.join(dir, entry.name);
log.info(`Watching ${file}`);
const job = this.safeGetJob(file);
if (job) watching.set(file, this.file(job));
}
}
const watcher = Deno.watchFs(dir, {recursive: true});
const promise = async () => {
for await (const event of watcher) {
if (event.paths.length < 1) continue;
const file = event.paths[0];
if (!file.endsWith("automkv.yml")) continue;
switch (event.kind) {
case "create": {
log.info(`Discovered ${file}`);
const job = this.safeGetJob(file);
if (job) watching.set(file, this.file(job));
break;
}
case "modify": {
log.info(`Updating ${file}`);
if (watching.has(file))
(watching.get(file) as Watch).cancel();
const job = this.safeGetJob(file);
if (job) watching.set(file, this.file(job));
break;
}
case "remove": {
log.info(`Removing ${file}`);
(watching.get(file) as Watch).cancel();
watching.delete(file);
break;
}
}
}
for (const watch of watching.values())
watch.cancel();
};
return {
cancel: () => watcher.close(),
promise: promise()
};
}
/**
* Watch a folder for changes. When any changes are detected in files that
* match the provided filter, then the update will fire. The update should
* return the number of times the file was modified.
*
* @param folder The folder to watch
* @param filter The filter to apply to files within that folder
* @param update The update to run - should return # of modifications
* @private Currently only used internally due to complexity
*/
private watch(folder: string, filter: RegExp, update: (file: string) => Promise<number>): Watch {
Deno.mkdirSync(folder, {recursive: true});
const watcher = Deno.watchFs(folder);
const promise = async () => {
const latch = new Map<string, number>();
for await (const event of watcher) {
if (event.paths.length < 1) continue;
const file = event.paths[0];
if (event.kind !== "modify") continue;
if (!filter.test(file)) continue;
/*
Countdown latch pattern: if an update makes multiple
modifications, then we want to wait for them all to finish
before watching for external changes.
*/
if (latch.has(file)) {
const countdown = latch.get(file) as number;
if (countdown <= 0) {
latch.delete(file);
} else {
latch.set(file, countdown - 1);
continue;
}
}
await Deno.open(file, {write: true})
.then(h => Deno.close(h.rid))
.then(() => update(file))
.then(i => latch.set(file, i))
.catch(() => log.warning(`Waiting for file to become ready: ${file}`));
}
};
return {
cancel: () => watcher.close(),
promise: promise()
}
}
/**
* Creates a Job from the specified file path, logging to the console if it
* fails and returning `undefined`.
*
* @param file The file to pass to Job
* @private Logs to console, internal use
*/
private safeGetJob(file: string): Job | undefined {
try {
return new Job(file);
} catch (e) {
if (e instanceof Job.InvalidJobException)
log.error(`Skipping invalid job ${file}: ${e.message}`);
else
throw e;
}
}
}