Skip to content

Commit c5fe2ae

Browse files
authored
fix(studio): register dynamically opened preview projects
1 parent ff6a382 commit c5fe2ae

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎packages/studio/vite.adapter.projects.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { createStudioApi, type ProjectHistory } from "@hyperframes/studio-server
1717
import type { ViteDevServer } from "vite";
1818
import { createProjectSignatureCache, createViteAdapter } from "./vite.adapter";
1919

20+
import { previewChangeOwner } from "./vite.preview-watch";
21+
2022
const roots: string[] = [];
2123
afterEach(() => {
2224
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
@@ -175,3 +177,35 @@ describe("Vite project resolution boundary", () => {
175177
expect(adapter.resolveProject("alias")).toBeNull();
176178
});
177179
});
180+
181+
describe("dynamic preview ownership", () => {
182+
it.each([false, true])("registers a project opened after startup (symlink: %s)", (linked) => {
183+
const { root, data, sessions } = fixture();
184+
const owners = new Map<string, string>();
185+
const watched: string[] = [];
186+
const cache = createProjectSignatureCache({
187+
compute: () => "signature",
188+
watch: (dir) => {
189+
// Ownership must exist before newly watched files can emit events.
190+
expect(previewChangeOwner(owners, join(dir, "index.html"))).toBe("new-project");
191+
watched.push(dir);
192+
},
193+
});
194+
const adapter = createViteAdapter(data, {} as ViteDevServer, cache, {
195+
onResolveProject: (project) => owners.set(project.dir, project.id),
196+
});
197+
expect(adapter.resolveProject("new-project")).toBeNull();
198+
expect(owners.size).toBe(0);
199+
const dir = linked ? join(root, "different-target-name") : join(data, "new-project");
200+
mkdirSync(dir);
201+
if (linked) symlinkSync(dir, join(data, "new-project"), "junction");
202+
writeFileSync(join(dir, "index.html"), "before");
203+
writeFileSync(join(sessions, "alias.json"), JSON.stringify({ projectId: "new-project" }));
204+
const project = adapter.resolveProject(linked ? "alias" : "new-project")!;
205+
cache.get(project.dir);
206+
writeFileSync(join(dir, "index.html"), "after");
207+
expect(watched).toEqual([realpathSync(dir)]);
208+
expect(previewChangeOwner(owners, join(realpathSync(dir), "index.html"))).toBe("new-project");
209+
expect(previewChangeOwner(owners, join(realpathSync(dir), "index.html.tmp"))).toBeNull();
210+
});
211+
});

‎packages/studio/vite.adapter.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ export function createViteAdapter(
107107
{
108108
historyRoot = DEFAULT_HISTORY_ROOT,
109109
openHistory = openProjectHistory,
110-
}: { historyRoot?: string; openHistory?: typeof openProjectHistory } = {},
110+
onResolveProject,
111+
}: {
112+
historyRoot?: string;
113+
openHistory?: typeof openProjectHistory;
114+
onResolveProject?: (project: ResolvedProject) => void;
115+
} = {},
111116
): StudioApiAdapter {
112117
const histories = historyCache((projectDir) =>
113118
openHistory({ projectDir, historyRoot }).catch((error: unknown) => {
@@ -240,11 +245,13 @@ export function createViteAdapter(
240245
projectDir = resolve(dataDir, session.projectId);
241246
if (!isPathWithin(dataDir, projectDir)) return null;
242247
if (existsSync(projectDir)) {
243-
return {
248+
const project = {
244249
id: session.projectId,
245250
dir: realpathSync(projectDir),
246251
title: session.title,
247252
};
253+
onResolveProject?.(project);
254+
return project;
248255
}
249256
}
250257
} catch {
@@ -253,7 +260,9 @@ export function createViteAdapter(
253260
}
254261
return null;
255262
}
256-
return { id, dir: realpathSync(projectDir) };
263+
const project = { id, dir: realpathSync(projectDir) };
264+
onResolveProject?.(project);
265+
return project;
257266
},
258267

259268
async bundle(dir, options) {

‎packages/studio/vite.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ function devProjectApi(): Plugin {
176176
// The engine records its write receipts in this module, where the watcher below reads them.
177177
const adapter = createViteAdapter(dataDir, server, signatureCache, {
178178
openHistory: mod.openProjectHistory,
179+
// Projects can be created or imported after startup. Keep the canonical
180+
// id and real root before the signature cache starts watching them.
181+
onResolveProject: (project) => watchedProjects.set(project.dir, project.id),
179182
});
180183
_api = mod.createStudioApi(adapter);
181184
}

0 commit comments

Comments
 (0)