SPB Git forge
38commits 1branches 0releases
338.7 MBsize
maindefault branch
4 h agolast push
HTML 53.9% TypeScript 44.5% JavaScript 0.6% SQL 0.5%
2.6 KB · 56 lines typescript
Raw Blame History
1/**2 * Registers every code-backed connector group: apps/worker/src/connectors/<group>/index.ts exporting3 * `register(): void` (which calls registerParser / registerImplementation from @dci/connectors).4 *5 * Groups are discovered on disk at runtime (readdirSync + dynamic import) so a missing or half-written group6 * never breaks the worker; failures are reported and the remaining groups still load. Add a static import7 * below as well when a group is stable (gives typecheck coverage) — the `once` guard makes double8 * registration harmless.9 */10import { existsSync, readdirSync } from "node:fs";11import { dirname, join } from "node:path";12import { fileURLToPath, pathToFileURL } from "node:url";1314// Static imports of known groups (kept in sync with the directories present on disk):15// import * as example from "./example/index.js";16const STATIC_GROUPS: Array<{ name: string; register: () => void }> = [];1718export interface RegisterReport { registered: string[]; failed: Array<{ group: string; error: string }> }1920let report: RegisterReport | null = null;21let pending: Promise<RegisterReport> | null = null;2223export async function registerAllConnectors(): Promise<RegisterReport> {24  if (report) return report;25  if (pending) return pending;26  pending = (async () => {27    const out: RegisterReport = { registered: [], failed: [] };28    const done = new Set<string>();29    for (const g of STATIC_GROUPS) {30      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 }); }31    }32    const dir = dirname(fileURLToPath(import.meta.url));33    if (existsSync(dir)) {34      for (const ent of readdirSync(dir, { withFileTypes: true })) {35        if (!ent.isDirectory() || done.has(ent.name)) continue;36        const idx = ["index.ts", "index.js"].map((f) => join(dir, ent.name, f)).find((p) => existsSync(p));37        if (!idx) continue;38        try {39          const mod = (await import(pathToFileURL(idx).href)) as { register?: () => void };40          if (typeof mod.register !== "function") { out.failed.push({ group: ent.name, error: "no register() export" }); continue; }41          mod.register();42          out.registered.push(ent.name);43        } catch (e) {44          const msg = (e as Error).message;45          // "already registered" means a static import (or another group) registered the same parser — not fatal46          if (/already registered/i.test(msg)) out.registered.push(`${ent.name} (dup)`);47          else out.failed.push({ group: ent.name, error: msg });48        }49      }50    }51    report = out;52    return out;53  })();54  return pending;55}56