prod setup

This commit is contained in:
Johan LEROY
2026-05-04 11:00:44 +02:00
parent 9e9bc0f23d
commit d30e8e9376
45 changed files with 1998 additions and 188 deletions

View File

@@ -1,14 +1,28 @@
import { readFile, writeFile } from 'node:fs/promises';
import { readFile, writeFile, mkdir, copyFile, access } from 'node:fs/promises';
import { join } from 'node:path';
import { CONTENT_SCHEMAS, type ContentKey } from '../content/schemas';
import type { z } from 'zod';
const CONTENT_DIR = join(process.cwd(), 'public', 'content');
const DATA_CONTENT_DIR = join(process.cwd(), 'data', 'content');
const SEED_CONTENT_DIR = join(process.cwd(), 'public', 'content');
type ContentData<K extends ContentKey> = z.infer<(typeof CONTENT_SCHEMAS)[K]>;
async function ensureContentFile(key: ContentKey): Promise<string> {
const target = join(DATA_CONTENT_DIR, `${key}.json`);
try {
await access(target);
return target;
} catch {
await mkdir(DATA_CONTENT_DIR, { recursive: true });
const seed = join(SEED_CONTENT_DIR, `${key}.json`);
await copyFile(seed, target);
return target;
}
}
export async function loadContent<K extends ContentKey>(key: K): Promise<ContentData<K>> {
const filePath = join(CONTENT_DIR, `${key}.json`);
const filePath = await ensureContentFile(key);
const raw = await readFile(filePath, 'utf-8');
const parsed = JSON.parse(raw);
const schema = CONTENT_SCHEMAS[key];
@@ -21,7 +35,7 @@ export async function saveContent<K extends ContentKey>(
): Promise<ContentData<K>> {
const schema = CONTENT_SCHEMAS[key];
const validated = schema.parse(data) as ContentData<K>;
const filePath = join(CONTENT_DIR, `${key}.json`);
const filePath = await ensureContentFile(key);
await writeFile(filePath, JSON.stringify(validated, null, 2) + '\n', 'utf-8');
return validated;
}