2026-11-03 12:24:43 +05:30
|
|
|
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');
|
2026-11-04 12:28:32 +05:30
|
|
|
const MANAGEMENT_DIR = path.join(process.cwd(), 'management');
|
2026-11-03 12:24:43 +05:30
|
|
|
|
|
|
|
|
// 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, '');
|
|
|
|
|
|
2026-11-04 12:28:32 +05:30
|
|
|
// 1. Prioritize management/pages/[name]/page.config.json
|
|
|
|
|
const mgmtPageFile = path.join(MANAGEMENT_DIR, 'pages', normSlug, 'page.config.json');
|
|
|
|
|
if (fs.existsSync(mgmtPageFile)) {
|
|
|
|
|
const page = readJsonFile<PageConfig>(mgmtPageFile);
|
|
|
|
|
if (page) return page;
|
2026-11-03 12:24:43 +05:30
|
|
|
}
|
2026-11-04 12:28:32 +05:30
|
|
|
|
|
|
|
|
// 2. Fallback to content/pages/[name].json
|
|
|
|
|
const filePath = path.join(CONTENT_DIR, 'pages', `${normSlug}.json`);
|
|
|
|
|
return readJsonFile<PageConfig>(filePath);
|
2026-11-03 12:24:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getAllTools(): ToolItem[] {
|
2026-11-04 15:36:30 +05:30
|
|
|
toolsCache.clear();
|
2026-11-04 12:28:32 +05:30
|
|
|
// 1. Prioritize management/pages/tools/[slug]/tool.config.json
|
|
|
|
|
const mgmtToolsDir = path.join(MANAGEMENT_DIR, 'pages', 'tools');
|
|
|
|
|
if (fs.existsSync(mgmtToolsDir)) {
|
|
|
|
|
const slugs = fs.readdirSync(mgmtToolsDir);
|
|
|
|
|
const tools: ToolItem[] = [];
|
|
|
|
|
for (const slug of slugs) {
|
|
|
|
|
const configPath = path.join(mgmtToolsDir, slug, 'tool.config.json');
|
|
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
|
const tool = readJsonFile<any>(configPath);
|
|
|
|
|
if (tool && tool.status === 'published') {
|
|
|
|
|
// Normalize media paths so before/after point to live management media
|
|
|
|
|
if (tool.media?.before && tool.content?.examples?.[0]) {
|
|
|
|
|
tool.content.examples[0].before = tool.media.before;
|
|
|
|
|
}
|
|
|
|
|
if (tool.media?.after && tool.content?.examples?.[0]) {
|
|
|
|
|
tool.content.examples[0].after = tool.media.after;
|
|
|
|
|
}
|
|
|
|
|
tools.push(tool);
|
|
|
|
|
toolsCache.set(tool.slug, tool);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (tools.length > 0) return tools;
|
|
|
|
|
}
|
2026-11-03 12:24:43 +05:30
|
|
|
|
2026-11-04 12:28:32 +05:30
|
|
|
// 2. Fallback to content/collections/tools
|
2026-11-03 12:24:43 +05:30
|
|
|
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[] {
|
2026-11-04 15:36:30 +05:30
|
|
|
appsCache.clear();
|
2026-11-04 12:28:32 +05:30
|
|
|
// 1. Prioritize management/apps/[slug]/app.config.json
|
|
|
|
|
const mgmtAppsDir = path.join(MANAGEMENT_DIR, 'apps');
|
|
|
|
|
if (fs.existsSync(mgmtAppsDir)) {
|
|
|
|
|
const slugs = fs.readdirSync(mgmtAppsDir);
|
|
|
|
|
const apps: AppItem[] = [];
|
|
|
|
|
for (const slug of slugs) {
|
|
|
|
|
const configPath = path.join(mgmtAppsDir, slug, 'app.config.json');
|
|
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
|
const app = readJsonFile<AppItem>(configPath);
|
|
|
|
|
if (app && app.status === 'published') {
|
|
|
|
|
apps.push(app);
|
|
|
|
|
appsCache.set(app.slug, app);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (apps.length > 0) return apps;
|
|
|
|
|
}
|
2026-11-03 12:24:43 +05:30
|
|
|
|
2026-11-04 12:28:32 +05:30
|
|
|
// 2. Fallback to content/collections/apps
|
2026-11-03 12:24:43 +05:30
|
|
|
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 {
|
2026-11-05 10:26:19 +05:30
|
|
|
const mgmtPath = path.join(MANAGEMENT_DIR, 'templates', `${name}.json`);
|
|
|
|
|
if (fs.existsSync(mgmtPath)) {
|
|
|
|
|
return readJsonFile(mgmtPath);
|
|
|
|
|
}
|
2026-11-03 12:24:43 +05:30
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|