fileFor
uses node:path
Where a new namespace's file goes in a mirror: next to a sibling namespace's file, in a parent's directory (or an index.ts for a namespace with children), or by the namespace's path from the root. Only a path the mirror reads back as that namespace, and that doesn't exist yet.
import { joinName, namespaceForFile } from "#codebase/tcmts/cli/mirror"; import type { Mirror } from "#codebase/tcmts/cli/mirror"; import fs from "node:fs"; import path from "node:path"; export function fileFor(m: Mirror, ns: string, jsx: boolean): string | undefined { const ext = jsx ? ".tsx" : ".ts"; const parent = ns.includes(".") ? ns.slice(0, ns.lastIndexOf(".")) : ""; const leaf = ns.slice(ns.lastIndexOf(".") + 1); const candidates: string[] = []; for (const [rel, f] of Object.entries(m.files)) { const dir = path.dirname(rel); const fParent = f.ns.includes(".") ? f.ns.slice(0, f.ns.lastIndexOf(".")) : ""; if (fParent === parent) candidates.push(path.join(dir, leaf + ext)); if (f.ns === parent) candidates.push(/^index\.(m|c)?tsx?$/.test(path.basename(rel)) ? path.join(dir, leaf + ext) : path.join(dir, path.basename(rel).replace(/\.(m|c)?tsx?$/, ""), leaf + ext)); if (fParent === ns) candidates.push(path.join(dir, "index" + ext)); } const rest = (m.into && ns.startsWith(`${m.into}.`) ? ns.slice(m.into.length + 1) : ns).split("."); candidates.push(path.join(...rest) + ext); return candidates.find((c) => joinName(m.into, namespaceForFile(m.dir, path.join(m.dir, c))) === ns && !fs.existsSync(path.join(m.dir, c))); }