followFileRename
uses node:fs
Imports and re-exports of the file from, renamed to (site.ts to site.tsx), pointed at its new name.
Returns the text of the files outside mirrored it changed, as they were.
import { applyEdits } from "#codebase/tcmts/cli/mirror"; import type { Edit } from "#codebase/tcmts/cli/mirror"; import { parse } from "#codebase/tcmts/core/ts"; import fs from "node:fs"; import path from "node:path"; import { isExportDeclaration as ts_isExportDeclaration, isImportDeclaration as ts_isImportDeclaration, isStringLiteral as ts_isStringLiteral, } from "typescript/ast"; export function followFileRename(files: string[], mirrored: Set<string>, from: string, to: string): Map<string, string> { const outside = new Map<string, string>(); for (const file of new Set(files)) { if (!fs.existsSync(file)) continue; const text = fs.readFileSync(file, "utf8"); const edits: Edit[] = []; for (const st of parse(file, text).statements) { const spec = (ts_isImportDeclaration(st) || ts_isExportDeclaration(st)) && st.moduleSpecifier && ts_isStringLiteral(st.moduleSpecifier) ? st.moduleSpecifier : undefined; if (!spec?.text.startsWith(".") || path.resolve(path.dirname(file), spec.text) !== from) continue; // the quotes kept, the name changed edits.push({ start: spec.end - 1 - path.basename(from).length, end: spec.end - 1, text: path.basename(to) }); } if (!edits.length) continue; if (!mirrored.has(file)) outside.set(file, text); fs.writeFileSync(file, applyEdits(text, edits)); } return outside; }