import fs from "node:fs"; import path from "node:path"; /** * Reads a feature.manifest.ts file and extracts the manifest's `name` field * and `requiredCores` array via regex. Returns null if the file does not * exist or does not match the expected literal `as const` manifest shape. * * The repo's convention is that every feature.manifest.ts uses defineFeature * with literal `as const` syntax — this is enforced by the type-system * design (defineFeature has `` to preserve literal types). The * regex extraction is therefore safe; the AST path is overkill. * * Returns: { name: string, requiredCores: string[] } | null */ export function readManifestSource(manifestPath) { let src; try { src = fs.readFileSync(manifestPath, "utf8"); } catch { return null; } const nameMatch = src.match(/name:\s*"([^"]+)"/); if (!nameMatch) return null; const coresMatch = src.match(/requiredCores:\s*\[([^\]]*)\]/); const cores = coresMatch ? coresMatch[1] .split(",") .map((s) => s.trim().replace(/^"/, "").replace(/"$/, "")) .filter((s) => s.length > 0) : []; return { name: nameMatch[1], requiredCores: cores }; } /** * Canonical manifest path for a given feature package root. */ export function manifestPathForFeature(featureRoot) { return path.join(featureRoot, "src", "feature.manifest.ts"); } /** * Given an absolute file path and the monorepo root, returns the feature * package root that contains the file (e.g. /repo/packages/auth). Returns * null when the file lives outside the packages/ tree. * * Assumes the conventional layout `packages//src/...`. */ export function featureRootForFile(filepath, repoRoot) { const packagesDir = path.join(repoRoot, "packages"); if (!filepath.startsWith(packagesDir + path.sep)) return null; const rel = filepath.slice(packagesDir.length + 1); // e.g. "auth/src/..." const slash = rel.indexOf(path.sep); if (slash === -1) return null; const featureName = rel.slice(0, slash); return path.join(packagesDir, featureName); }