|
| 1 | +import { existsSync, readdirSync, readFileSync } from "node:fs"; |
| 2 | +import { dirname, join } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 6 | +const ROOT = join(__dirname, "../.."); |
| 7 | +export const PDFS_DIR = join(ROOT, "src/pdfs"); |
| 8 | + |
| 9 | +interface PdfMeta { |
| 10 | + title: string; |
| 11 | + code: string; |
| 12 | + jurisdiction: string; |
| 13 | +} |
| 14 | + |
| 15 | +export interface PdfEntry { |
| 16 | + id: string; |
| 17 | + jurisdiction: string; |
| 18 | + title: string; |
| 19 | + code: string; |
| 20 | + fieldCount: number; |
| 21 | + pdfPath: string; |
| 22 | + pdfDir: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** Parses index.ts for PDF metadata via regex. */ |
| 26 | +function parsePdfMetadata(pdfDir: string): PdfMeta | null { |
| 27 | + const indexPath = join(pdfDir, "index.ts"); |
| 28 | + if (!existsSync(indexPath)) return null; |
| 29 | + const content = readFileSync(indexPath, "utf8"); |
| 30 | + return { |
| 31 | + title: content.match(/\btitle:\s*"([^"]+)"/)?.[1] ?? "", |
| 32 | + code: content.match(/\bcode:\s*"([^"]+)"/)?.[1] ?? "", |
| 33 | + jurisdiction: content.match(/\bjurisdiction:\s*"([^"]+)"/)?.[1] ?? "", |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** Counts fields in schema.ts by counting PDF class references. */ |
| 38 | +function countSchemaFields(pdfDir: string): number { |
| 39 | + const schemaPath = join(pdfDir, "schema.ts"); |
| 40 | + if (!existsSync(schemaPath)) return 0; |
| 41 | + const content = readFileSync(schemaPath, "utf8"); |
| 42 | + return ( |
| 43 | + content.match(/:\s*PDF(?:TextField|CheckBox|RadioGroup|Dropdown)/g) ?? [] |
| 44 | + ).length; |
| 45 | +} |
| 46 | + |
| 47 | +export function listAllPdfs(): PdfEntry[] { |
| 48 | + const result: PdfEntry[] = []; |
| 49 | + for (const entry of readdirSync(PDFS_DIR, { withFileTypes: true })) { |
| 50 | + if (!entry.isDirectory() || entry.name === "utils") continue; |
| 51 | + const jurisdictionDir = join(PDFS_DIR, entry.name); |
| 52 | + for (const pdfEntry of readdirSync(jurisdictionDir, { |
| 53 | + withFileTypes: true, |
| 54 | + })) { |
| 55 | + if (!pdfEntry.isDirectory()) continue; |
| 56 | + const pdfDir = join(jurisdictionDir, pdfEntry.name); |
| 57 | + const pdfFile = readdirSync(pdfDir).find((f) => f.endsWith(".pdf")); |
| 58 | + if (!pdfFile) continue; |
| 59 | + const meta = parsePdfMetadata(pdfDir); |
| 60 | + if (!meta) continue; |
| 61 | + result.push({ |
| 62 | + id: pdfEntry.name, |
| 63 | + jurisdiction: meta.jurisdiction || entry.name.toUpperCase(), |
| 64 | + title: meta.title, |
| 65 | + code: meta.code, |
| 66 | + fieldCount: countSchemaFields(pdfDir), |
| 67 | + pdfPath: join(pdfDir, pdfFile), |
| 68 | + pdfDir, |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + return result; |
| 73 | +} |
| 74 | + |
| 75 | +export function findPdfById( |
| 76 | + id: string | undefined, |
| 77 | +): { pdfDir: string; pdfPath: string } | null { |
| 78 | + if (!id || id.includes("..")) return null; |
| 79 | + for (const entry of readdirSync(PDFS_DIR, { withFileTypes: true })) { |
| 80 | + if (!entry.isDirectory() || entry.name === "utils") continue; |
| 81 | + const pdfDir = join(PDFS_DIR, entry.name, id); |
| 82 | + if (!existsSync(pdfDir)) continue; |
| 83 | + const pdfFile = readdirSync(pdfDir).find((f) => f.endsWith(".pdf")); |
| 84 | + if (pdfFile) return { pdfDir, pdfPath: join(pdfDir, pdfFile) }; |
| 85 | + } |
| 86 | + return null; |
| 87 | +} |
0 commit comments