|
| 1 | +import { existsSync } from "fs"; |
| 2 | +import { resolve } from "path"; |
| 3 | +import type { CatalogModule } from "./base.js"; |
| 4 | +import { packageCatalog } from "./package.js"; |
| 5 | +import { gitCatalog } from "./git.js"; |
| 6 | +import { dockerCatalog } from "./docker.js"; |
| 7 | +import { fsCatalog } from "./fs.js"; |
| 8 | +import { shellCatalog } from "./shell.js"; |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// All available catalogs |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +const ALL_CATALOGS: CatalogModule[] = [ |
| 14 | + packageCatalog, |
| 15 | + gitCatalog, |
| 16 | + dockerCatalog, |
| 17 | + fsCatalog, |
| 18 | + shellCatalog, |
| 19 | +]; |
| 20 | + |
| 21 | +const CATALOG_MAP = new Map<string, CatalogModule>( |
| 22 | + ALL_CATALOGS.map((c) => [c.name, c]), |
| 23 | +); |
| 24 | + |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | +// Auto-detect active catalogs based on project structure |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +export function detectCatalogs( |
| 29 | + cwd: string = process.cwd(), |
| 30 | + forcedCatalogs?: string[], |
| 31 | +): CatalogModule[] { |
| 32 | + // If forced catalogs specified, validate and use only those |
| 33 | + if (forcedCatalogs && forcedCatalogs.length > 0) { |
| 34 | + const active: CatalogModule[] = []; |
| 35 | + for (const name of forcedCatalogs) { |
| 36 | + const catalog = CATALOG_MAP.get(name); |
| 37 | + if (!catalog) { |
| 38 | + const valid = Array.from(CATALOG_MAP.keys()).join(", "); |
| 39 | + throw new Error(`Unknown catalog "${name}". Valid: ${valid}`); |
| 40 | + } |
| 41 | + active.push(catalog); |
| 42 | + } |
| 43 | + return active; |
| 44 | + } |
| 45 | + |
| 46 | + // Auto-detect mode |
| 47 | + const active: CatalogModule[] = []; |
| 48 | + |
| 49 | + for (const catalog of ALL_CATALOGS) { |
| 50 | + // Always include catalogs with no detectors (fs, shell) |
| 51 | + if (catalog.detectors.length === 0) { |
| 52 | + active.push(catalog); |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + // Check if any detector exists |
| 57 | + const detected = catalog.detectors.some((detector) => { |
| 58 | + const path = resolve(cwd, detector); |
| 59 | + return existsSync(path); |
| 60 | + }); |
| 61 | + |
| 62 | + if (detected) { |
| 63 | + active.push(catalog); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return active; |
| 68 | +} |
| 69 | + |
| 70 | +// --------------------------------------------------------------------------- |
| 71 | +// Build prompt from active catalogs |
| 72 | +// --------------------------------------------------------------------------- |
| 73 | +export function buildCatalogPrompt( |
| 74 | + cwd: string = process.cwd(), |
| 75 | + forcedCatalogs?: string[], |
| 76 | +): string { |
| 77 | + const active = detectCatalogs(cwd, forcedCatalogs); |
| 78 | + const lines = active.map((catalog) => catalog.buildPrompt()); |
| 79 | + return `Allowed command types and commands:\n${lines.join("\n")}`; |
| 80 | +} |
| 81 | + |
| 82 | +// --------------------------------------------------------------------------- |
| 83 | +// Get all type enum values from active catalogs |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +export function getAllTypeEnums( |
| 86 | + cwd: string = process.cwd(), |
| 87 | + forcedCatalogs?: string[], |
| 88 | +): string[] { |
| 89 | + const active = detectCatalogs(cwd, forcedCatalogs); |
| 90 | + const types: string[] = []; |
| 91 | + for (const catalog of active) { |
| 92 | + types.push(...catalog.typeEnum); |
| 93 | + } |
| 94 | + return [...new Set(types)]; // dedupe |
| 95 | +} |
| 96 | + |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | +// Build command map for validation |
| 99 | +// --------------------------------------------------------------------------- |
| 100 | +export function buildCommandMap( |
| 101 | + cwd: string = process.cwd(), |
| 102 | + forcedCatalogs?: string[], |
| 103 | +): Map<string, readonly string[] | ["any"]> { |
| 104 | + const active = detectCatalogs(cwd, forcedCatalogs); |
| 105 | + const map = new Map<string, readonly string[] | ["any"]>(); |
| 106 | + |
| 107 | + for (const catalog of active) { |
| 108 | + for (const type of catalog.typeEnum) { |
| 109 | + map.set(type, catalog.commands); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return map; |
| 114 | +} |
| 115 | + |
| 116 | +// --------------------------------------------------------------------------- |
| 117 | +// Re-export base types and functions |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +export { |
| 120 | + createStepSchema, |
| 121 | + createPlanSchema, |
| 122 | + validateStep, |
| 123 | + type CatalogModule, |
| 124 | + type Step, |
| 125 | + type Plan, |
| 126 | +} from "./base.js"; |
| 127 | + |
| 128 | +// Export individual catalogs for advanced use |
| 129 | +export { packageCatalog, gitCatalog, dockerCatalog, fsCatalog, shellCatalog }; |
0 commit comments