fix(studio-server): rename a nested folder rewrites the paths files beside it use

This commit is contained in:
Miguel Angel Simon Sierra
2026-10-03 09:03:31 -07:00
parent b3c5207827
commit e511bfef22
2 changed files with 57 additions and 3 deletions
@@ -234,4 +234,37 @@ describe("renaming a folder over the route", () => {
expect(response.status).toBe(200);
expect(readFileSync(join(project, "index.html"), "utf8")).toBe('<img src="logo.png">');
});
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"),
'<img src="assets/clips/a.png"><img src="clips/a.png">',
);
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(
'<img src="assets/takes/a.png"><img src="clips/a.png">',
);
});
});
+24 -3
View File
@@ -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, (text: string) => 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++;