-
Notifications
You must be signed in to change notification settings - Fork 3
/
watch.mjs
42 lines (35 loc) · 1.25 KB
/
watch.mjs
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
import fsCallback from 'node:fs';
import { buildComponentSets, listComponentSetPaths, validateComponentSet } from './build.mjs';
export async function watchComponentSets() {
// Watch does not rebuild vendor.js and design.css
await buildComponentSets();
for (const componentSetPath of await listComponentSetPaths()) {
await watchComponentSet(componentSetPath);
}
}
export async function watchComponentSet(componentSetPath) {
let dirty = false;
let activePromise = null;
const doValidateComponentSet = () => {
dirty = false;
activePromise = validateComponentSet(componentSetPath)
.catch((err) => {
console.error(err);
})
.finally(() => {
activePromise = undefined;
if (dirty) {
doValidateComponentSet();
}
});
};
fsCallback.watch(componentSetPath, { recursive: true }, (eventType, filename) => {
console.log(`\n${new Date().toLocaleString()}\nThe file "${filename}" was modified!`);
console.log('The type of change was:', eventType);
if (activePromise) {
dirty = true;
} else {
doValidateComponentSet();
}
});
}