/** * Registers every code-backed connector group: apps/worker/src/connectors//index.ts exporting * `register(): void` (which calls registerParser / registerImplementation from @dci/connectors). * * Groups are discovered on disk at runtime (readdirSync + dynamic import) so a missing or half-written group * never breaks the worker; failures are reported and the remaining groups still load. Add a static import * below as well when a group is stable (gives typecheck coverage) — the `once` guard makes double * registration harmless. */ import { existsSync, readdirSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; // Static imports of known groups (kept in sync with the directories present on disk): // import * as example from "./example/index.js"; const STATIC_GROUPS: Array<{ name: string; register: () => void }> = []; export interface RegisterReport { registered: string[]; failed: Array<{ group: string; error: string }> } let report: RegisterReport | null = null; let pending: Promise | null = null; export async function registerAllConnectors(): Promise { if (report) return report; if (pending) return pending; pending = (async () => { const out: RegisterReport = { registered: [], failed: [] }; const done = new Set(); for (const g of STATIC_GROUPS) { try { g.register(); out.registered.push(g.name); done.add(g.name); } catch (e) { out.failed.push({ group: g.name, error: (e as Error).message }); } } const dir = dirname(fileURLToPath(import.meta.url)); if (existsSync(dir)) { for (const ent of readdirSync(dir, { withFileTypes: true })) { if (!ent.isDirectory() || done.has(ent.name)) continue; const idx = ["index.ts", "index.js"].map((f) => join(dir, ent.name, f)).find((p) => existsSync(p)); if (!idx) continue; try { const mod = (await import(pathToFileURL(idx).href)) as { register?: () => void }; if (typeof mod.register !== "function") { out.failed.push({ group: ent.name, error: "no register() export" }); continue; } mod.register(); out.registered.push(ent.name); } catch (e) { const msg = (e as Error).message; // "already registered" means a static import (or another group) registered the same parser — not fatal if (/already registered/i.test(msg)) out.registered.push(`${ent.name} (dup)`); else out.failed.push({ group: ent.name, error: msg }); } } } report = out; return out; })(); return pending; }