55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { promises as fs } from "node:fs";
|
|
import path from "node:path";
|
|
import { CatalogExportQueue } from "./catalog-git-core";
|
|
|
|
export function catalogExportEnabled() {
|
|
return Boolean(process.env.CATALOG_GIT_CHECKOUT?.trim());
|
|
}
|
|
export function catalogExportQueue() {
|
|
return new CatalogExportQueue(
|
|
process.env.CATALOG_GIT_STATE_DIR ||
|
|
path.join(process.cwd(), "storage", "catalog-git"),
|
|
);
|
|
}
|
|
export function isCatalogMutation(method: string, pathname: string) {
|
|
return (
|
|
["POST", "PUT", "PATCH", "DELETE"].includes(method) &&
|
|
(pathname.startsWith("/api/admin/import/") ||
|
|
pathname.startsWith("/api/admin/catalog/") ||
|
|
pathname.startsWith("/api/admin/furni/") ||
|
|
pathname.startsWith("/api/admin/furniture/"))
|
|
);
|
|
}
|
|
export async function catalogExportStatus() {
|
|
const q = catalogExportQueue();
|
|
if (!catalogExportEnabled()) return { enabled: false, pending: 0, active: 0 };
|
|
const entries = await q.entries();
|
|
let last: { commit?: string; finishedAt?: string; error?: string } = {};
|
|
try {
|
|
last = JSON.parse(
|
|
await fs.readFile(path.join(q.root, "status.json"), "utf8"),
|
|
);
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
}
|
|
return {
|
|
enabled: true,
|
|
pending: entries.filter((f) => f.endsWith(".pending")).length,
|
|
active: entries.filter((f) => f.endsWith(".active")).length,
|
|
running: entries.includes("worker.lock"),
|
|
...last,
|
|
};
|
|
}
|
|
|
|
export async function withCatalogExport<T>(
|
|
operation: () => Promise<T>,
|
|
): Promise<T> {
|
|
if (!catalogExportEnabled()) return operation();
|
|
const finish = await catalogExportQueue().begin();
|
|
try {
|
|
return await operation();
|
|
} finally {
|
|
await finish();
|
|
}
|
|
}
|