154 lines
4.7 KiB
TypeScript
154 lines
4.7 KiB
TypeScript
import fs from 'fs';
|
|||
|
|
import path from 'path';
|
||
|
|
import { SiteConfig, PageConfig, ToolItem, AppItem, CategoryItem } from '@/types';
|
||
|
|
import { getEndpointById } from '../catalog/loader';
|
||
|
|
|
||
|
|
const CONTENT_DIR = path.join(process.cwd(), 'content');
|
||
|
|
|
||
|
|
// In-memory cache for speed and resilience
|
||
|
|
let siteConfigCache: SiteConfig | null = null;
|
||
|
|
let pagesCache = new Map<string, PageConfig>();
|
||
|
|
let toolsCache = new Map<string, ToolItem>();
|
||
|
|
let appsCache = new Map<string, AppItem>();
|
||
|
|
let categoriesCache = new Map<string, CategoryItem>();
|
||
|
|
|
||
|
|
function readJsonFile<T>(filePath: string): T | null {
|
||
|
|
try {
|
||
|
|
if (!fs.existsSync(filePath)) return null;
|
||
|
|
const raw = fs.readFileSync(filePath, 'utf8');
|
||
|
|
return JSON.parse(raw) as T;
|
||
|
|
} catch (e) {
|
||
|
|
console.error(`Failed to read/parse JSON from ${filePath}:`, e);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getSiteConfig(): SiteConfig {
|
||
|
|
if (siteConfigCache) return siteConfigCache;
|
||
|
|
const config = readJsonFile<SiteConfig>(path.join(CONTENT_DIR, 'site.config.json'));
|
||
|
|
if (!config) {
|
||
|
|
throw new Error('Crucial site.config.json missing or invalid');
|
||
|
|
}
|
||
|
|
siteConfigCache = config;
|
||
|
|
return config;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getPageConfig(slug: string): PageConfig | null {
|
||
|
|
const normSlug = slug === '' || slug === '/' ? 'home' : slug.replace(/^\/+|\/+$/g, '');
|
||
|
|
if (pagesCache.has(normSlug)) return pagesCache.get(normSlug)!;
|
||
|
|
|
||
|
|
const filePath = path.join(CONTENT_DIR, 'pages', `${normSlug}.json`);
|
||
|
|
const page = readJsonFile<PageConfig>(filePath);
|
||
|
|
if (page) {
|
||
|
|
pagesCache.set(normSlug, page);
|
||
|
|
}
|
||
|
|
return page;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getAllTools(): ToolItem[] {
|
||
|
|
if (toolsCache.size > 0) return Array.from(toolsCache.values());
|
||
|
|
|
||
|
|
const toolsDir = path.join(CONTENT_DIR, 'collections', 'tools');
|
||
|
|
if (fs.existsSync(toolsDir)) {
|
||
|
|
const files = fs.readdirSync(toolsDir).filter((f) => f.endsWith('.json'));
|
||
|
|
for (const file of files) {
|
||
|
|
const tool = readJsonFile<ToolItem>(path.join(toolsDir, file));
|
||
|
|
if (tool && tool.status === 'published') {
|
||
|
|
toolsCache.set(tool.slug, tool);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return Array.from(toolsCache.values());
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getToolBySlug(slug: string): ToolItem | null {
|
||
|
|
getAllTools();
|
||
|
|
return toolsCache.get(slug) || null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getToolById(id: string): ToolItem | null {
|
||
|
|
getAllTools();
|
||
|
|
for (const t of toolsCache.values()) {
|
||
|
|
if (t.id === id) return t;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getAllApps(): AppItem[] {
|
||
|
|
if (appsCache.size > 0) return Array.from(appsCache.values());
|
||
|
|
|
||
|
|
const appsDir = path.join(CONTENT_DIR, 'collections', 'apps');
|
||
|
|
if (fs.existsSync(appsDir)) {
|
||
|
|
const files = fs.readdirSync(appsDir).filter((f) => f.endsWith('.json'));
|
||
|
|
for (const file of files) {
|
||
|
|
const app = readJsonFile<AppItem>(path.join(appsDir, file));
|
||
|
|
if (app && app.status === 'published') {
|
||
|
|
appsCache.set(app.slug, app);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return Array.from(appsCache.values());
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getAppBySlug(slug: string): AppItem | null {
|
||
|
|
getAllApps();
|
||
|
|
return appsCache.get(slug) || null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getAppById(id: string): AppItem | null {
|
||
|
|
getAllApps();
|
||
|
|
for (const a of appsCache.values()) {
|
||
|
|
if (a.id === id) return a;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getAllCategories(): CategoryItem[] {
|
||
|
|
if (categoriesCache.size > 0) return Array.from(categoriesCache.values());
|
||
|
|
|
||
|
|
const dir = path.join(CONTENT_DIR, 'collections', 'categories');
|
||
|
|
if (fs.existsSync(dir)) {
|
||
|
|
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.json'));
|
||
|
|
for (const file of files) {
|
||
|
|
const cat = readJsonFile<CategoryItem>(path.join(dir, file));
|
||
|
|
if (cat) {
|
||
|
|
categoriesCache.set(cat.slug, cat);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return Array.from(categoriesCache.values());
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getTemplate(name: string): any {
|
||
|
|
const filePath = path.join(CONTENT_DIR, 'templates', `${name}.json`);
|
||
|
|
return readJsonFile(filePath);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getLocaleDictionary(locale: string = 'en'): Record<string, string> {
|
||
|
|
const filePath = path.join(CONTENT_DIR, 'locales', `${locale}.json`);
|
||
|
|
const data = readJsonFile<{ system: Record<string, string> }>(filePath);
|
||
|
|
return data?.system || {};
|
||
|
|
}
|
||
|
|
|
||
|
|
export function checkIntegrity(): { valid: boolean; errors: string[] } {
|
||
|
|
const errors: string[] = [];
|
||
|
|
const tools = getAllTools();
|
||
|
|
const apps = getAllApps();
|
||
|
|
const appIds = new Set(apps.map((a) => a.id));
|
||
|
|
|
||
|
|
for (const tool of tools) {
|
||
|
|
if (!appIds.has(tool.homeApp)) {
|
||
|
|
errors.push(`Tool '${tool.id}' references missing homeApp '${tool.homeApp}'`);
|
||
|
|
}
|
||
|
|
const epId = tool.tryIt?.endpointRef?.id;
|
||
|
|
if (epId && !getEndpointById(epId)) {
|
||
|
|
errors.push(`Tool '${tool.id}' references missing endpoint '${epId}'`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
valid: errors.length === 0,
|
||
|
|
errors,
|
||
|
|
};
|
||
|
|
}
|