|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { DOCS_DIR } from "@/config"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Definition for a guideline resource. |
| 7 | + */ |
| 8 | +export interface GuidelineResource { |
| 9 | + /** Unique resource name */ |
| 10 | + name: string; |
| 11 | + /** Resource URI (e.g., mendix://guidelines/frontend) */ |
| 12 | + uri: string; |
| 13 | + /** Human-readable title */ |
| 14 | + title: string; |
| 15 | + /** Description of what this guideline covers */ |
| 16 | + description: string; |
| 17 | + /** Source markdown file name */ |
| 18 | + filename: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * All available guideline resources. |
| 23 | + */ |
| 24 | +export const GUIDELINE_RESOURCES: GuidelineResource[] = [ |
| 25 | + { |
| 26 | + name: "frontend-guidelines", |
| 27 | + uri: "mendix://guidelines/frontend", |
| 28 | + title: "Frontend Guidelines", |
| 29 | + description: "CSS/SCSS styling, naming conventions, component best practices, and Atlas UI integration", |
| 30 | + filename: "frontend-guidelines.md" |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "implementation-plan", |
| 34 | + uri: "mendix://guidelines/implementation", |
| 35 | + title: "Implementation Plan", |
| 36 | + description: "Step-by-step guide for creating new widgets, including PR templates and testing requirements", |
| 37 | + filename: "implementation-plan.md" |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "app-flow", |
| 41 | + uri: "mendix://guidelines/app-flow", |
| 42 | + title: "Application Flow", |
| 43 | + description: "Complete widget development lifecycle from scaffolding to Studio Pro integration", |
| 44 | + filename: "app-flow.md" |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "backend-structure", |
| 48 | + uri: "mendix://guidelines/backend-structure", |
| 49 | + title: "Backend Structure", |
| 50 | + description: |
| 51 | + "Widget-to-Mendix runtime integration, data handling with EditableValue/ActionValue, and event management", |
| 52 | + filename: "backend-structure.md" |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "tech-stack", |
| 56 | + uri: "mendix://guidelines/tech-stack", |
| 57 | + title: "Technology Stack", |
| 58 | + description: "Core technologies (TypeScript, React, SCSS), monorepo structure, and development tools", |
| 59 | + filename: "tech-stack.md" |
| 60 | + } |
| 61 | +]; |
| 62 | + |
| 63 | +/** |
| 64 | + * Cache for loaded guideline content to avoid repeated file reads. |
| 65 | + */ |
| 66 | +const guidelineCache = new Map<string, string>(); |
| 67 | + |
| 68 | +/** |
| 69 | + * Loads the content of a guideline file. |
| 70 | + * Caches content after first load for performance. |
| 71 | + * |
| 72 | + * @param filename - The markdown filename to load |
| 73 | + * @returns The file content as a string |
| 74 | + */ |
| 75 | +export async function loadGuidelineContent(filename: string): Promise<string> { |
| 76 | + // Check cache first |
| 77 | + if (guidelineCache.has(filename)) { |
| 78 | + return guidelineCache.get(filename)!; |
| 79 | + } |
| 80 | + |
| 81 | + const filePath = join(DOCS_DIR, filename); |
| 82 | + |
| 83 | + try { |
| 84 | + const content = await readFile(filePath, "utf-8"); |
| 85 | + guidelineCache.set(filename, content); |
| 86 | + return content; |
| 87 | + } catch (error) { |
| 88 | + const message = error instanceof Error ? error.message : String(error); |
| 89 | + throw new Error(`Failed to load guideline ${filename}: ${message}`); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Clears the guideline cache. Useful for testing or hot-reloading. |
| 95 | + */ |
| 96 | +export function clearGuidelineCache(): void { |
| 97 | + guidelineCache.clear(); |
| 98 | +} |
0 commit comments