Add schema validation and sync metadata UI

This commit is contained in:
SepComet
2026-05-06 09:47:00 +08:00
parent e03af0c800
commit 85e510fbc0
14 changed files with 474 additions and 126 deletions
+40 -30
View File
@@ -1,26 +1,16 @@
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { fetchGiteaSnapshot, type GeneratedGiteaActivity, type GeneratedProject } from './fetch-gitea.ts';
import { fetchSeafileSnapshot, type GeneratedShare } from './fetch-seafile.ts';
type ProjectSeed = {
name: string;
description: string;
gitea_repo: string;
cover_image: string;
demo_video?: string;
download_link?: string;
tags: string[];
featured: boolean;
};
type ShareSeed = {
name: string;
description?: string;
url?: string;
time?: string;
};
import { fetchGiteaSnapshot, type GeneratedGiteaActivity } from './fetch-gitea.ts';
import { fetchSeafileSnapshot } from './fetch-seafile.ts';
import {
generatedProjectListSchema,
generatedShareListSchema,
giteaActivitySchema,
parseWithSchema,
projectSeedListSchema,
shareSeedListSchema,
} from '../src/data/schema.ts';
type SyncConfig = {
rootDir: string;
@@ -60,11 +50,15 @@ async function main() {
);
}
const seedProjects = await readJson<ProjectSeed[]>(
path.join(config.rootDir, 'src/content/projects/index.json'),
const seedProjects = parseWithSchema(
projectSeedListSchema,
await readJson<unknown>(path.join(config.rootDir, 'src/content/projects/index.json')),
'seed projects',
);
const seedShares = await readJson<ShareSeed[]>(
path.join(config.rootDir, 'src/content/shares/index.json'),
const seedShares = parseWithSchema(
shareSeedListSchema,
await readJson<unknown>(path.join(config.rootDir, 'src/content/shares/index.json')),
'seed shares',
);
const giteaSnapshot = hasGiteaConfig(config)
@@ -111,15 +105,31 @@ async function main() {
recent: [],
} satisfies GeneratedGiteaActivity);
const validatedProjects = parseWithSchema(
generatedProjectListSchema,
seafileSnapshot.projects,
'generated projects',
);
const validatedShares = parseWithSchema(
generatedShareListSchema,
seafileSnapshot.shares,
'generated shares',
);
const validatedActivity = parseWithSchema(
giteaActivitySchema,
giteaActivity,
'generated gitea activity',
);
await Promise.all([
writeJson(path.join(config.outputDir, 'projects.json'), seafileSnapshot.projects),
writeJson(path.join(config.outputDir, 'shares.json'), seafileSnapshot.shares),
writeJson(path.join(config.outputDir, 'gitea-activity.json'), giteaActivity),
writeJson(path.join(config.outputDir, 'projects.json'), validatedProjects),
writeJson(path.join(config.outputDir, 'shares.json'), validatedShares),
writeJson(path.join(config.outputDir, 'gitea-activity.json'), validatedActivity),
]);
console.log(`[sync-content] wrote ${seafileSnapshot.projects.length} projects`);
console.log(`[sync-content] wrote ${seafileSnapshot.shares.length} shares`);
console.log(`[sync-content] wrote ${giteaActivity.itemCount} activity items`);
console.log(`[sync-content] wrote ${validatedProjects.length} projects`);
console.log(`[sync-content] wrote ${validatedShares.length} shares`);
console.log(`[sync-content] wrote ${validatedActivity.itemCount} activity items`);
console.log('[sync-content] done');
}