Seed template recommendations

This commit is contained in:
adilallo
2026-05-23 19:35:38 -06:00
parent 6d0335a86a
commit f0e193746c
6 changed files with 45 additions and 18 deletions
+3 -6
View File
@@ -10,17 +10,14 @@ import {
resolveFacetMatch,
sectionFacetsSchema,
} from "../../lib/server/validation/methodFacetsSchemas";
// Bundled seed runs from repo root (`process.cwd()`); __dirname breaks under esbuild.
const REPO_ROOT = process.cwd();
const DATA_DIR = path.join(REPO_ROOT, "data", "create", "customRule");
import { methodFacetsJsonDir } from "./seedDataPaths";
/**
* Reads + Zod-validates `data/create/customRule/<section>.json`.
* Throws on schema failures so the seed aborts before any DB write.
*/
async function loadSectionFacets(section: SectionId) {
const file = path.join(DATA_DIR, `${section}.json`);
const file = path.join(methodFacetsJsonDir(), `${section}.json`);
const raw = await readFile(file, "utf8");
const parsed = JSON.parse(raw) as unknown;
const result = sectionFacetsSchema.safeParse(parsed);
@@ -33,7 +30,7 @@ async function loadSectionFacets(section: SectionId) {
}
async function loadFacetGroups() {
const file = path.join(DATA_DIR, "_facetGroups.json");
const file = path.join(methodFacetsJsonDir(), "_facetGroups.json");
const raw = await readFile(file, "utf8");
const parsed = JSON.parse(raw) as unknown;
const result = facetGroupsFileSchema.safeParse(parsed);
+22
View File
@@ -0,0 +1,22 @@
import path from "node:path";
/**
* Root for committed seed JSON (`data/` in dev; `/app/seed-data` on Cloudron).
*
* Cloudron's localstorage addon mounts `/app/data` at runtime, so facet JSON
* must not live there. The Dockerfile copies `data/` → `/app/seed-data` and
* sets `SEED_DATA_DIR=/app/seed-data`.
*/
export function seedDataRoot(): string {
const override = process.env.SEED_DATA_DIR?.trim();
if (override) return override;
return path.join(process.cwd(), "data");
}
export function methodFacetsJsonDir(): string {
return path.join(seedDataRoot(), "create", "customRule");
}
export function templateFacetJsonPath(): string {
return path.join(seedDataRoot(), "templates", "templateFacet.json");
}
+4 -11
View File
@@ -1,16 +1,8 @@
import { readFile } from "node:fs/promises";
import path from "node:path";
import type { PrismaClient } from "@prisma/client";
import { FACET_GROUP_IDS } from "../../lib/server/validation/methodFacetsSchemas";
import { templateFacetFileSchema } from "../../lib/server/validation/templateFacetSchema";
const REPO_ROOT = process.cwd();
const TEMPLATE_FACET_FILE = path.join(
REPO_ROOT,
"data",
"templates",
"templateFacet.json",
);
import { templateFacetJsonPath } from "./seedDataPaths";
type TemplateFacetRow = {
templateSlug: string;
@@ -20,12 +12,13 @@ type TemplateFacetRow = {
};
async function loadTemplateFacets() {
const raw = await readFile(TEMPLATE_FACET_FILE, "utf8");
const templateFacetFile = templateFacetJsonPath();
const raw = await readFile(templateFacetFile, "utf8");
const parsed = JSON.parse(raw) as unknown;
const result = templateFacetFileSchema.safeParse(parsed);
if (!result.success) {
throw new Error(
`Invalid template facet file ${TEMPLATE_FACET_FILE}: ${JSON.stringify(
`Invalid template facet file ${templateFacetFile}: ${JSON.stringify(
result.error.flatten(),
null,
2,