diff --git a/packages/studio-server/src/routes/files.renameReferences.test.ts b/packages/studio-server/src/routes/files.renameReferences.test.ts index ca36ba442f..63f09e94c5 100644 --- a/packages/studio-server/src/routes/files.renameReferences.test.ts +++ b/packages/studio-server/src/routes/files.renameReferences.test.ts @@ -234,4 +234,37 @@ describe("renaming a folder over the route", () => { expect(response.status).toBe(200); expect(readFileSync(join(project, "index.html"), "utf8")).toBe(''); }); + + it("rewrites a path a file in the renamed folder's parent names relative to itself", async () => { + const project = mkdtempSync(join(tmpdir(), "hf-rename-refs-")); + dirs.push(project); + mkdirSync(join(project, "assets", "clips"), { recursive: true }); + writeFileSync(join(project, "assets", "clips", "a.png"), "x"); + writeFileSync( + join(project, "assets", "style.css"), + "a{background:url(clips/a.png)} b{x:url(./clips/a.png)}", + ); + writeFileSync( + join(project, "index.html"), + '', + ); + const adapter = { + resolveProject: async (id: string) => ({ id, dir: project }), + } as unknown as StudioApiAdapter; + const app = new Hono(); + registerFileRoutes(app, adapter); + + const response = await app.request("/projects/p/files/assets/clips", { + method: "PATCH", + body: JSON.stringify({ newPath: "assets/takes" }), + }); + + expect(response.status).toBe(200); + expect(readFileSync(join(project, "assets", "style.css"), "utf8")).toBe( + "a{background:url(takes/a.png)} b{x:url(./takes/a.png)}", + ); + expect(readFileSync(join(project, "index.html"), "utf8")).toBe( + '', + ); + }); }); diff --git a/packages/studio-server/src/routes/files.ts b/packages/studio-server/src/routes/files.ts index 9202fd74fa..d7782d86f8 100644 --- a/packages/studio-server/src/routes/files.ts +++ b/packages/studio-server/src/routes/files.ts @@ -20,7 +20,7 @@ import { realpathSync, type Dirent, } from "node:fs"; -import { resolve, dirname, join } from "node:path"; +import { resolve, dirname, join, relative, posix, sep } from "node:path"; import type { StudioApiAdapter } from "../types.js"; import { isAudioFile } from "../helpers/mime.js"; import { createFileAtomically, replaceFileAtomically } from "@hyperframes/core/atomic-file"; @@ -773,14 +773,35 @@ function updateReferences( /\.(html|css|js|jsx|ts|tsx|json|mjs|cjs|md|mdx)$/i.test(name), ); - const rewrite = referenceRewriter(oldPath, newPath, isDirectory, projectPaths(projectDir)); + const existing = projectPaths(projectDir); + const rewrite = referenceRewriter(oldPath, newPath, isDirectory, existing); + const nearby = new Map string>(); + const rewriteFor = (dir: string) => { + const [from, to] = [posix.relative(dir, oldPath), posix.relative(dir, newPath)]; + const moved = dir === newPath || dir.startsWith(`${newPath}/`); + if (dir === "" || moved || from.replace(/^(\.\.\/)+/, "") === oldPath) return rewrite; + const cached = + nearby.get(dir) ?? + nearby + .set( + dir, + referenceRewriter( + from, + to, + isDirectory, + existing.map((path) => posix.relative(dir, path)), + ), + ) + .get(dir)!; + return (text: string) => cached(rewrite(text)); + }; let updatedCount = 0; for (const file of textFiles) { if (!isSafePath(projectDir, file)) continue; const content = readableText(file); if (content === null) continue; - const updated = rewrite(content); + const updated = rewriteFor(relative(projectDir, dirname(file)).split(sep).join("/"))(content); if (updated !== content) { replaceFileAtomically(file, updated, statSync(file).mode); updatedCount++;