/** * Assemble connectors/registry.json from every connectors///meta.json. * Per-connector meta files avoid merge conflicts; registry.json stays the machine-readable * registry required by CLAUDE.md §106. Derived fields (domain, country, capabilities, refreshClass) * are filled in here so meta.json stays short. Run: pnpm tsx scripts/build-registry.ts [--check] */ import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { RegistrySchema, ConnectorMetaSchema, capabilitiesOf, domainOf, effectiveRefreshClass, type ConnectorMeta } from '../packages/connectors/src/types.ts'; import { SourcesFileSchema } from '../packages/connectors/src/sources.ts'; import { DomainsFileSchema, DOMAINS_DIR } from '../packages/connectors/src/domains.ts'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const dir = path.join(root, 'connectors'); const check = process.argv.includes('--check'); const metas: ConnectorMeta[] = []; const problems: string[] = []; for (const engine of readdirSync(dir)) { const p = path.join(dir, engine); if (!statSync(p).isDirectory() || engine === 'node_modules' || engine.startsWith('_')) continue; for (const id of readdirSync(p)) { if (!statSync(path.join(p, id)).isDirectory() || id.startsWith('_')) continue; const metaPath = path.join(p, id, 'meta.json'); try { const meta = JSON.parse(readFileSync(metaPath, 'utf8')); const parsed = ConnectorMetaSchema.parse({ ...meta, module: meta.module ?? `${engine}/${id}` }); if (parsed.id !== id) throw new Error(`meta.id ${parsed.id} ≠ folder ${id}`); parsed.domain ??= domainOf(parsed.sourceUrl); parsed.country ??= parsed.regions[0] ?? 'global'; parsed.capabilities = capabilitiesOf(parsed); parsed.refreshClass = effectiveRefreshClass(parsed); if (!existsSync(path.join(p, id, 'index.ts'))) problems.push(`${engine}/${id}: index.ts missing`); if (!existsSync(path.join(p, id, 'index.test.ts'))) problems.push(`${engine}/${id}: index.test.ts missing (SPEC §14)`); const fx = path.join(root, 'data/fixtures', id); if (!existsSync(fx) || !readdirSync(fx).some((f) => f.endsWith('.json'))) problems.push(`${engine}/${id}: no fixture in data/fixtures/${id} (SPEC §14)`); if (!parsed.accessNotes) problems.push(`${engine}/${id}: accessNotes missing (legal/compliance notes, SPEC §36)`); metas.push(parsed); } catch (err) { if ((err as NodeJS.ErrnoException).code === 'ENOENT') continue; throw new Error(`${metaPath}: ${err instanceof Error ? err.message : String(err)}`); } } } metas.sort((a, b) => (a.id < b.id ? -1 : 1)); const registry = RegistrySchema.parse({ version: '1.1', connectors: metas }); // Cross-check with the source registry (SPEC §4): every connector's sourceId should be a documented source. const sourcesPath = path.join(root, 'data/sources/sources.json'); if (existsSync(sourcesPath)) { const sources = SourcesFileSchema.parse(JSON.parse(readFileSync(sourcesPath, 'utf8'))); const ids = new Set(sources.sources.map((s) => s.id)); for (const m of metas) if (!ids.has(m.sourceId) && !ids.has(m.id)) problems.push(`${m.id}: sourceId "${m.sourceId}" has no entry in data/sources/sources.json`); } // Strict validation of domain-policy fragments (runtime skips invalid ones with a warning). if (existsSync(DOMAINS_DIR)) { for (const f of readdirSync(DOMAINS_DIR).filter((x) => x.endsWith('.json'))) { const r = DomainsFileSchema.safeParse({ ...JSON.parse(readFileSync(path.join(DOMAINS_DIR, f), 'utf8')), version: '1.0' }); if (!r.success) problems.push(`connectors/domains.d/${f}: ${r.error.issues[0]?.path.join('.')} ${r.error.issues[0]?.message}`); } } if (!check) writeFileSync(path.join(dir, 'registry.json'), JSON.stringify(registry, null, 2) + '\n'); console.log(`[registry] ${registry.connectors.length} connectors ${check ? 'validated' : 'written to connectors/registry.json'}`); if (problems.length) { console.log(`[registry] ${problems.length} completeness warning(s):`); for (const p of problems) console.log(` - ${p}`); if (check && process.argv.includes('--strict')) process.exit(1); }