The VS Code of video editing, built for agents.
TypeScript
Refactor project renaming logic and documentation
konstantin-paulus450495c44bb3
diff --git a/apps/desktop/src/main-channels.ts b/apps/desktop/src/main-channels.tsindex 17d1bc16..e7aed12d 100644--- a/apps/desktop/src/main-channels.ts+++ b/apps/desktop/src/main-channels.ts@@ -255,7 +255,7 @@ export type MainRequestMap = {request: { root: string; displayName: string };response: ProjectInfo;};- // Renames the project: `displayName` in the record, and the folder with it.+ // Renames the project: `displayName` in the record. The folder is left alone.[MAIN_CHANNELS.PROJECTS_RENAME]: {request: { dir: string; displayName: string };response: ProjectInfo;diff --git a/apps/desktop/src/projects.ts b/apps/desktop/src/projects.tsindex 6f6cef2c..53894f99 100644--- a/apps/desktop/src/projects.ts+++ b/apps/desktop/src/projects.ts@@ -6,7 +6,7 @@ import { app, dialog, shell, type BrowserWindow } from "electron";import { execFile } from "node:child_process";import { createHash } from "node:crypto";import { watch, type FSWatcher } from "node:fs";-import { cp, mkdir, readdir, readFile, realpath, rename, rm, stat } from "node:fs/promises";+import { cp, mkdir, readdir, readFile, realpath, rm, stat } from "node:fs/promises";import { createRequire } from "node:module";import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";import { nanoid } from "nanoid";@@ -98,7 +98,8 @@ async function writePackage(dir: string, pkg: PackageJson): Promise<void> {// Identity// A project is named twice: `displayName` is what the user calls it and the-// folder is what the disk calls it, and renaming moves both. Neither can be+// folder is what the disk calls it, and either can change apart from the+// other (the app renames the one, the user moves the other). Neither can be// the project's identity, so the record carries an id that nothing renames —// it is what the app's URLs point at, and what the editor keys the open world// by. Top level rather than inside `diffusion`, which `writeConfig` replaces@@ -757,50 +758,19 @@ export async function createProject(root: string, displayName: string): Promise<}/**- * Renames the project: the human name in the record, and the folder with it,- * so what the user sees in Finder keeps matching what they see in the app.- * This is the only thing here that moves a folder — nothing syncs the two- * behind the user's back — and it costs the project neither its identity nor- * its links, both of which are the id.+ * Renames the project: the human name in the record, and nothing else. The+ * folder keeps its name — moving it is the user's to do, by hand — and the id+ * the project goes by is untouched either way.*/export async function renameProject(dir: string, displayName: string): Promise<ProjectInfo> {const pkg = (await readPackage(dir)) ?? packageJson(basename(dir), displayName);- const name = displayName.trim() || basename(dir);- await writePackage(dir, { ...pkg, displayName: name });+ await writePackage(dir, { ...pkg, displayName: displayName.trim() || basename(dir) });- const project = await describe(await renameFolder(dir, name));+ const project = await describe(dir);if (!project) throw new Error("Not a project folder.");return project;}-/**- * Moves the project folder to match `displayName`; answers where it ended up.- * Best effort by design: the folder can be locked, open elsewhere, or on a- * volume that will not have it, and none of that should cost the user the- * name they just typed.- */-async function renameFolder(dir: string, displayName: string): Promise<string> {- const root = dirname(dir);- const base = folderName(displayName);- const current = basename(dir);- // Already named after it, numbered suffix and all: moving `my-clip-2` to- // `my-clip` (or to `my-clip-3`) shuffles folders that are equally right.- if (current === base || (current.startsWith(`${base}-`) && /^\d+$/.test(current.slice(base.length + 1)))) {- return dir;- }-- try {- const target = join(root, await freeFolder(root, base));- // The watcher holds the old path, and the renderer re-watches the new one- // as soon as it hears where the project went.- unwatchProject(dir);- await rename(dir, target);- return target;- } catch {- return dir;- }-}-/** Copies the folder next to itself as `<name>-copy` (numbered when taken). */export async function duplicateProject(dir: string): Promise<ProjectInfo> {const source = await describe(dir);@@ -811,9 +781,7 @@ export async function duplicateProject(dir: string): Promise<ProjectInfo> {await cp(dir, target, { recursive: true, errorOnExist: true, force: false });// The record came along with everything else, id included, and two folders- // answering to one id is the thing the id exists to prevent. The folder is- // already named for the copy, so the record is written here rather than- // through `renameProject` (which would move it again).+ // answering to one id is the thing the id exists to prevent.const pkg = await readPackage(target);await writePackage(target, {...(pkg ?? packageJson(basename(target), source.displayName)),diff --git a/apps/web/src/components/dashboard/project-card.tsx b/apps/web/src/components/dashboard/project-card.tsxindex 8ab0a436..898261b3 100644--- a/apps/web/src/components/dashboard/project-card.tsx+++ b/apps/web/src/components/dashboard/project-card.tsx@@ -32,11 +32,7 @@ type DashboardProjectCardProps = {onOpen: () => void;/** Delete was pressed on the card, or chosen from the menu. */onDelete: () => void;- /**- * A rename or duplicate changed the projects on disk; the owner refetches- * its list. Called after a rename regardless of outcome, since the folder- * moves with the name and the list is stale either way.- */+ /** A rename or duplicate changed the projects on disk; the owner refetches its list. */onChanged: () => void;};@@ -94,7 +90,6 @@ export function DashboardProjectCard(props: DashboardProjectCardProps) {const handleBlurRenameInput = () => {stopRenaming();- props.onChanged();};const handleKeyDownRenameInput = async (event: KeyboardEvent) => {@@ -106,7 +101,6 @@ export function DashboardProjectCard(props: DashboardProjectCardProps) {event.stopPropagation();stopRenaming();- props.onChanged();input.blur();}@@ -114,18 +108,16 @@ export function DashboardProjectCard(props: DashboardProjectCardProps) {event.preventDefault();event.stopPropagation();- // The folder moves with the name, so the list is refetched rather than- // patched: every path in it has just changed.if (trimmedName.length > 0 && renaming()) {try {await renameProject(props.project.dir, trimmedName);+ props.onChanged();} catch (e) {toast.error("Failed to rename project", { description: (e as Error).message });}}stopRenaming();- props.onChanged();input.blur();}};diff --git a/apps/web/src/components/sidebar-left/sidebar-left.tsx b/apps/web/src/components/sidebar-left/sidebar-left.tsxindex f10141dc..baddf7b4 100644--- a/apps/web/src/components/sidebar-left/sidebar-left.tsx+++ b/apps/web/src/components/sidebar-left/sidebar-left.tsx@@ -121,8 +121,6 @@ function ProjectNameInput(props: ProjectNameInputProps) {const input = event.currentTarget;const trimmedName = projectNameDraft()?.trim() ?? "";- // The rename the folder follows: the project keeps its id, so the URL- // and the open editor are untouched by the move.if (trimmedName.length > 0 && trimmedName !== project.name()) {try {await project.rename(trimmedName);diff --git a/apps/web/src/context/project.tsx b/apps/web/src/context/project.tsxindex ca75dd02..5f8cdc2a 100644--- a/apps/web/src/context/project.tsx+++ b/apps/web/src/context/project.tsx@@ -4,10 +4,10 @@// The open project, for everything that acts on it. A project is three names// at once and they come apart: the id is what it *is* (the URL, the key the-// world is under), the folder is where it lives (and moves, when the project-// is renamed), and the display name is what the user calls it. Resolving the-// URL to a project happens once, in ProjectPage; this is that answer, kept-// current as the project is renamed underneath the editor.+// world is under), the folder is where it lives, and the display name is what+// the user calls it. Resolving the URL to a project happens once, in+// ProjectPage; this is that answer, kept current as the project is renamed+// underneath the editor.import { createContext, createMemo, createSignal, useContext, type Accessor, type JSX } from 'solid-js';@@ -18,11 +18,11 @@ import type { ProjectInfo } from '@/projects';export type ProjectContextValue = {/** package.json `projectId`: what the project is, whatever it is called. */id: Accessor<string>;- /** Absolute project folder. Moves when the project is renamed. */+ /** Absolute project folder. */dir: Accessor<string>;/** package.json `displayName`: what the user calls it. */name: Accessor<string>;- /** Renames the project — the record, and the folder with it. */+ /** Renames the project — the record; the folder keeps its name. */rename: (displayName: string) => Promise<void>;/** Re-reads the record, for when something else wrote it. */refresh: () => Promise<void>;@@ -35,8 +35,8 @@ export function ProjectProvider(props: { project: ProjectInfo; children: JSX.Ele// arrives as a new provider rather than as a new value here.const [info, setInfo] = createSignal(props.project);- // Memos, not plain reads: `dir` re-mounts the scene when it changes, and a- // refresh that found nothing new must not look like a move.+ // Memos, not plain reads: a refresh that found nothing new must not look+ // like a change.const id = createMemo(() => info().id);const dir = createMemo(() => info().dir);const name = createMemo(() => info().displayName);diff --git a/apps/web/src/lib/db.ts b/apps/web/src/lib/db.tsindex 25dad417..afc95485 100644--- a/apps/web/src/lib/db.ts+++ b/apps/web/src/lib/db.ts@@ -192,22 +192,6 @@ export async function findProjectRecords(ref: string): Promise<ProjectRecord[]>];}-/**- * Re-keys the record for `from` to `to`, for a project whose folder moved- * (renaming one moves its folder). Nothing to do when `from` is not on record.- */-export async function moveProjectRecord(from: string, to: string): Promise<void> {- if (from === to) return;- const db = await dbPromise;- const tx = db.transaction('projects', 'readwrite');- const existing = await tx.store.get(from);- if (existing) {- await tx.store.delete(from);- await tx.store.put({ ...existing, dir: to });- }- await tx.done;-}-/*** Records `cover` as the picture of the project in `dir`, replacing the one* before it. Nothing to do when the project is not on record: one deleteddiff --git a/apps/web/src/pages/editor.tsx b/apps/web/src/pages/editor.tsxindex ed279bf3..51c31177 100644--- a/apps/web/src/pages/editor.tsx+++ b/apps/web/src/pages/editor.tsx@@ -42,9 +42,8 @@ export function EditorPage() {const world = useWorld();const engine = useEngineContext();- // Keyed on the folder, not the project: a rename moves it, and everything- // below holds a path — the watcher, the library, the writer — so all of it- // is torn down and re-attached where the project now is.+ // Everything below holds the folder's path — the watcher, the library, the+ // writer — and is torn down with the editor.createEffect(() => {const dir = project.dir();if (!dir) return;diff --git a/apps/web/src/pages/project.tsx b/apps/web/src/pages/project.tsxindex cd760649..534e3364 100644--- a/apps/web/src/pages/project.tsx+++ b/apps/web/src/pages/project.tsx@@ -46,11 +46,7 @@ export function ProjectPage() {});return (- /**- * keyed so the engine provider is remounted when the project changes —- * on the project, not on its folder: renaming one must not tear down the- * world the user is working in.- */+ /** keyed so the engine provider is remounted when the project changes. */<Show when={!project.loading}><Show when={project()} keyed fallback={<Navigate href="/" />}>{(found) => (diff --git a/apps/web/src/projects/host.ts b/apps/web/src/projects/host.tsindex 209bf2ad..4e4b5bcf 100644--- a/apps/web/src/projects/host.ts+++ b/apps/web/src/projects/host.ts@@ -12,7 +12,6 @@ import {findProjectRecords,forgetProject as forgetProjectRecord,listProjectRecords,- moveProjectRecord,rememberProject,updateProjectRecord,} from '@/lib/db';@@ -234,17 +233,11 @@ export async function getProject(dir: string): Promise<ProjectInfo | null> {return mainBridge.call(MAIN_CHANNELS.PROJECTS_GET, { dir });}-/**- * Renames the project: `displayName` in the record, and the folder with it.- * The folder moves, so the answer says where the project now lives — hold on- * to it. Its id has not changed, and neither has its URL; the list follows- * the folder.- */+/** Renames the project: `displayName` in the record. The folder keeps its name. */export async function renameProject(dir: string, displayName: string): Promise<ProjectInfo> {if (!dir) throw new Error('No project folder.');const project = await mainBridge.call(MAIN_CHANNELS.PROJECTS_RENAME, { dir, displayName });- await moveProjectRecord(dir, project.dir);await remember(project);return project;}