fix
This commit is contained in:
parent
e170d69336
commit
9715ca9f4b
|
|
@ -1,14 +1,13 @@
|
||||||
import watcher, { AsyncSubscription } from "@parcel/watcher";
|
|
||||||
import { readdir } from "node:fs/promises";
|
|
||||||
import { statSync } from "node:fs";
|
|
||||||
import { join } from "path";
|
|
||||||
import { code } from "../code";
|
|
||||||
import { user } from "../../entity/user";
|
|
||||||
import { conns } from "../../entity/conn";
|
import { conns } from "../../entity/conn";
|
||||||
|
import { user } from "../../entity/user";
|
||||||
import { sendWS } from "../../sync-handler";
|
import { sendWS } from "../../sync-handler";
|
||||||
import { SyncType } from "../../type";
|
import { SyncType } from "../../type";
|
||||||
|
import { FSWatcher, statSync, watch } from "fs";
|
||||||
|
import { readdir } from "node:fs/promises";
|
||||||
|
import { code } from "../code";
|
||||||
|
import { join } from "path";
|
||||||
export class Watcher {
|
export class Watcher {
|
||||||
subcription = null as null | AsyncSubscription;
|
watchers = {} as Record<string, FSWatcher>;
|
||||||
|
|
||||||
constructor(path: string, id_site: string) {
|
constructor(path: string, id_site: string) {
|
||||||
this.init(path, id_site);
|
this.init(path, id_site);
|
||||||
|
|
@ -34,51 +33,52 @@ export class Watcher {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.subcription = await watcher.subscribe(path, (err, events) => {
|
const createWatcher = (p: string, recursive: boolean) => {
|
||||||
for (const e of events) {
|
return watch(p, { recursive }, (e, filename) => {
|
||||||
const filename = e.path.substring(path.length + 1);
|
const fe = code.internal.frontend[id_site];
|
||||||
if (e.type === "create" || e.type === "update") {
|
|
||||||
const fe = code.internal.frontend[id_site];
|
|
||||||
if (
|
|
||||||
filename?.startsWith("node_modules") ||
|
|
||||||
filename?.startsWith("typings") ||
|
|
||||||
filename?.endsWith(".log")
|
|
||||||
)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
filename?.endsWith(".tsx") ||
|
filename?.endsWith(".tsx") ||
|
||||||
filename?.endsWith(".ts") ||
|
filename?.endsWith(".ts") ||
|
||||||
filename?.endsWith(".css") ||
|
filename?.endsWith(".css") ||
|
||||||
filename?.endsWith(".html")
|
filename?.endsWith(".html")
|
||||||
) {
|
) {
|
||||||
if (typeof fe !== "undefined" && !fe.rebuilding) {
|
if (typeof fe !== "undefined" && !fe.rebuilding) {
|
||||||
fe.rebuilding = true;
|
fe.rebuilding = true;
|
||||||
clearTimeout(fe.timeout);
|
clearTimeout(fe.timeout);
|
||||||
fe.timeout = setTimeout(async () => {
|
fe.timeout = setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
broadcastLoading();
|
broadcastLoading();
|
||||||
await fe.ctx.rebuild();
|
await fe.ctx.rebuild();
|
||||||
fe.rebuilding = false;
|
fe.rebuilding = false;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(`Frontend failed rebuild (site: ${id_site})`);
|
console.error(`Frontend failed rebuild (site: ${id_site})`);
|
||||||
console.error(e.message);
|
console.error(e.message);
|
||||||
fe.rebuilding = false;
|
fe.rebuilding = false;
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.watchers["."] = createWatcher(path, false);
|
||||||
|
const files = await readdir(path);
|
||||||
|
for (const file of files) {
|
||||||
|
if (file.startsWith(".") || file === "node_modules") continue;
|
||||||
|
const stats = statSync(file);
|
||||||
|
if (stats.isDirectory()) {
|
||||||
|
this.watchers[file] = createWatcher(join(path, file), true);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
if (this.subcription) {
|
for (const v of Object.values(this.watchers)) {
|
||||||
await this.subcription.unsubscribe();
|
v.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue