Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 3x 1x 4x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x | import * as fs from "node:fs" import * as path from "node:path" import { parse as parseYaml } from "yaml" import type { ArchitectureDiagramKind, ArchitectureDiagramMeta, ArchitectureDiagramsPayload, LoadedArchitectureDiagram, } from "./domain/architecture-diagram.js" const DIAGRAM_EXTENSIONS = new Set([".mmd", ".md"]) export function diagramsDirectory(docsRoot: string): string { return path.join(docsRoot, "architecture", "diagrams") } export function loadArchitectureDiagramsPayload( docsRoot: string, ): ArchitectureDiagramsPayload { const dir = diagramsDirectory(docsRoot) if (!fs.existsSync(dir)) { return { diagrams: [] } } const files = fs .readdirSync(dir) .filter((name) => DIAGRAM_EXTENSIONS.has(path.extname(name).toLowerCase())) .sort((a, b) => a.localeCompare(b)) const diagrams: LoadedArchitectureDiagram[] = files.map((fileName) => { const absolutePath = path.join(dir, fileName) const relativePath = path.join("architecture", "diagrams", fileName).replace(/\\/g, "/") try { const raw = fs.readFileSync(absolutePath, "utf8") const ext = path.extname(fileName).toLowerCase() if (ext === ".mmd") { return loadMmdFile(fileName, relativePath, absolutePath, raw) } return loadMdFile(fileName, relativePath, absolutePath, raw) } catch (error) { const message = error instanceof Error ? error.message : String(error) return emptyDiagram(fileName, relativePath, absolutePath, message) } }) return { diagrams } } function loadMmdFile( fileName: string, relativePath: string, absolutePath: string, raw: string, ): LoadedArchitectureDiagram { const mermaid = raw.trim() if (!mermaid) { throw new Error("Empty .mmd file") } return finalizeDiagram(fileName, relativePath, absolutePath, { title: titleFromFileName(fileName) }, mermaid) } function loadMdFile( fileName: string, relativePath: string, absolutePath: string, raw: string, ): LoadedArchitectureDiagram { const { frontmatter, body } = splitFrontmatter(raw) const meta = parseMeta(frontmatter, fileName) const mermaid = extractMermaidBlock(body) if (!mermaid) { throw new Error("No ```mermaid code block found in .md file") } return finalizeDiagram(fileName, relativePath, absolutePath, meta, mermaid) } function emptyDiagram( fileName: string, relativePath: string, absolutePath: string, error: string, ): LoadedArchitectureDiagram { return { fileName, relativePath, absolutePath, meta: { title: titleFromFileName(fileName), kind: inferKindFromFileName(fileName) }, mermaid: null, error, } } function finalizeDiagram( fileName: string, relativePath: string, absolutePath: string, meta: ArchitectureDiagramMeta, mermaid: string, ): LoadedArchitectureDiagram { return { fileName, relativePath, absolutePath, meta: { ...meta, kind: meta.kind ?? inferKindFromFileName(fileName), }, mermaid, error: null, } } export function splitFrontmatter(raw: string): { frontmatter: string; body: string } { const match = raw.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/) if (!match) { return { frontmatter: "", body: raw } } return { frontmatter: match[1] ?? "", body: match[2] ?? "" } } export function parseMeta(frontmatter: string, fileName: string): ArchitectureDiagramMeta { if (!frontmatter.trim()) { return { title: titleFromFileName(fileName) } } const doc = parseYaml(frontmatter) as Record<string, unknown> | null if (!doc || typeof doc !== "object") { return { title: titleFromFileName(fileName) } } const title = typeof doc.title === "string" && doc.title.trim() ? doc.title.trim() : titleFromFileName(fileName) return { title, subtitle: typeof doc.subtitle === "string" ? doc.subtitle : undefined, source_doc: typeof doc.source_doc === "string" ? doc.source_doc : undefined, updated: typeof doc.updated === "string" ? doc.updated : undefined, kind: parseKind(doc.kind) ?? inferKindFromFileName(fileName), } } const DIAGRAM_KINDS = new Set<ArchitectureDiagramKind>([ "runtime", "database", "integration", "security", "flow", "other", ]) export function parseKind(value: unknown): ArchitectureDiagramKind | undefined { if (typeof value !== "string") { return undefined } const normalized = value.trim().toLowerCase() return DIAGRAM_KINDS.has(normalized as ArchitectureDiagramKind) ? (normalized as ArchitectureDiagramKind) : undefined } export function inferKindFromFileName(fileName: string): ArchitectureDiagramKind { const lower = fileName.toLowerCase() if (/(database|schema|er-|er_|delivery-store)/.test(lower)) { return "database" } if (/(runtime|system|overview|context)/.test(lower)) { return "runtime" } if (/(integration|api|contract)/.test(lower)) { return "integration" } if (/(security|auth|trust)/.test(lower)) { return "security" } if (/(flow|sequence|state)/.test(lower)) { return "flow" } return "other" } export function kindLabel(kind: ArchitectureDiagramKind | undefined): string { switch (kind) { case "runtime": return "Runtime" case "database": return "Database" case "integration": return "Integration" case "security": return "Security" case "flow": return "Flow" default: return "Diagram" } } export function extractMermaidBlock(body: string): string | null { const match = body.match(/```mermaid\s*\r?\n([\s\S]*?)```/i) return match?.[1]?.trim() ?? null } export function titleFromFileName(fileName: string): string { const base = path.basename(fileName, path.extname(fileName)) return base .split(/[-_]+/) .filter(Boolean) .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) .join(" ") } |