Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add PROJECTS_SCAN channel and implement project scanning functionality

konstantin-paulusb36383c50f8b
diff --git a/apps/desktop/src/main-channels.ts b/apps/desktop/src/main-channels.ts
index ed7bfe81..d385815b 100644
--- a/apps/desktop/src/main-channels.ts
+++ b/apps/desktop/src/main-channels.ts
@@ -44,6 +44,7 @@ export const MAIN_CHANNELS = {
PROJECTS_PICK_FOLDER: "projects:pick-folder",
PROJECTS_DEFAULT_ROOT: "projects:default-root",
PROJECTS_LIST: "projects:list",
+ PROJECTS_SCAN: "projects:scan",
PROJECTS_GET: "projects:get",
PROJECTS_INIT: "projects:init",
PROJECTS_RESOLVE: "projects:resolve",
@@ -225,6 +226,12 @@ export type MainRequestMap = {
request: { dirs: string[] };
response: ProjectInfo[];
};
+ // Every project folder directly under `root`: what the app puts on its
+ // list when `root` is chosen as the projects root.
+ [MAIN_CHANNELS.PROJECTS_SCAN]: {
+ request: { root: string };
+ response: ProjectInfo[];
+ };
[MAIN_CHANNELS.PROJECTS_GET]: {
request: { dir: string };
response: ProjectInfo | null;
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index a3b1f346..150f46f4 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -33,6 +33,7 @@ import {
pickRoot,
renameProject,
resolveProject,
+ scanProjects,
unwatchAll,
listEntries,
realPathEntry,
@@ -311,6 +312,7 @@ if (app.requestSingleInstanceLock()) {
mainBridge.handle(MAIN_CHANNELS.PROJECTS_PICK_FOLDER, () => pickFolder(mainWindow));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_DEFAULT_ROOT, () => defaultRoot(mainWindow));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_LIST, ({ dirs }) => listProjects(dirs));
+ mainBridge.handle(MAIN_CHANNELS.PROJECTS_SCAN, ({ root }) => scanProjects(root));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_GET, ({ dir }) => getProject(dir));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_INIT, ({ dir }) => initProject(mainWindow, dir));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_RESOLVE, ({ dir }) => resolveProject(dir));
diff --git a/apps/desktop/src/projects.ts b/apps/desktop/src/projects.ts
index ae8ff3c0..5134d5f7 100644
--- a/apps/desktop/src/projects.ts
+++ b/apps/desktop/src/projects.ts
@@ -400,6 +400,26 @@ export async function listProjects(dirs: string[]): Promise<ProjectInfo[]> {
return projects.filter((project): project is ProjectInfo => project !== null);
}
+/** Direct child folders of `root` that could hold a project, in a stable order. */
+async function childDirs(root: string): Promise<string[]> {
+ const entries = await readdir(root, { withFileTypes: true });
+ return entries
+ .filter((entry) => entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules")
+ .map((entry) => entry.name)
+ .sort()
+ .map((name) => join(root, name));
+}
+
+/**
+ * Every direct child folder of `root` that holds an entry file. The one
+ * search of the disk for projects there is: the app runs it when a projects
+ * root is chosen, to put the projects already in it on its list. Reads only.
+ */
+export async function scanProjects(root: string): Promise<ProjectInfo[]> {
+ const projects = await Promise.all((await childDirs(root)).map(describe));
+ return projects.filter((project): project is ProjectInfo => project !== null);
+}
+
/**
* The project in `dir`, left holding an id — this is where a folder that
* predates ids, or was made by hand, gets one — so the caller can send the
diff --git a/apps/web/src/components/dashboard/projects-folder-bar.tsx b/apps/web/src/components/dashboard/projects-folder-bar.tsx
index 82cf44d4..d9601894 100644
--- a/apps/web/src/components/dashboard/projects-folder-bar.tsx
+++ b/apps/web/src/components/dashboard/projects-folder-bar.tsx
@@ -8,7 +8,7 @@ import { toast } from "somoto";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";
import { revealPath } from "@/lib/shell";
-import { isDesktop, pickProjectsRoot, projectsRoot, rootsReady } from "@/projects";
+import { isDesktop, pickProjectsRoot, projectsRoot } from "@/projects";
/**
* Footer bar of the projects view: shows the projects root and the actions on
@@ -16,9 +16,7 @@ import { isDesktop, pickProjectsRoot, projectsRoot, rootsReady } from "@/project
* browser build (see @/projects).
*/
export function DashboardProjectsFolderBar() {
- // Blank rather than "No folder selected" until the database has answered:
- // the root arrives a tick after the bar first renders.
- const rootLabel = () => (rootsReady() ? projectsRoot() ?? "No folder selected" : "");
+ const rootLabel = () => projectsRoot() ?? "No folder selected";
const hasRoot = () => !!projectsRoot();
const handleChange = async () => {
@@ -52,7 +50,7 @@ export function DashboardProjectsFolderBar() {
/>
</span>
<div class="flex min-w-0 flex-1 flex-col justify-center gap-1">
- <p class="h-4 text-xs text-foreground">Project folder</p>
+ <p class="h-4 text-xs text-foreground">Projects folder</p>
<p class="min-w-0 truncate text-xs text-muted-foreground">
{rootLabel()}
</p>
diff --git a/apps/web/src/lib/db.ts b/apps/web/src/lib/db.ts
index b75a0c86..2ddab9f0 100644
--- a/apps/web/src/lib/db.ts
+++ b/apps/web/src/lib/db.ts
@@ -4,7 +4,6 @@
import { openDB } from 'idb';
import type * as idb from 'idb';
-import { nanoid } from 'nanoid';
const adjectives = ["Golden", "Silent", "Fast", "Bright", "Dark", "Wild", "Calm"];
const nouns = ["River", "Mountain", "Dream", "Storm", "Sunset", "Forest", "Ocean"];
@@ -23,32 +22,16 @@ export function generateProjectName(): string {
}
/**
- * A folder new projects are created in (desktop; see @/projects). A path
- * rather than a handle: on desktop the main process does the creating, and
- * it takes paths. Creating is the folder's one job — nothing looks inside it.
- * The projects the app shows are the ones it has on record (`ProjectRecord`),
- * wherever they live.
+ * A project the app knows: one it created, one it was asked to open — from
+ * the folder picker, or `dapi open <path>` — or one found in the projects
+ * root when that was chosen. The dashboard lists these and nothing else.
+ * Keyed by folder, which is how main addresses a project. The id is a copy of
+ * what the folder's package.json said last time — enough to find the folder
+ * a URL names without reading every record's package.json — and the folder
+ * has the final say (see `resolveProject` in @/projects).
*
- * Stored as a list because there will be several. The app works against one
- * at a time for now — the most recently used, so no separate "active" flag
- * can end up pointing at a root that was removed.
- */
-export interface ProjectRoot {
- id: string;
- path: string; // Absolute path of the folder.
- name: string;
- createdAt: string;
- lastUsedAt: string;
-}
-
-/**
- * A project the app knows: one it created, or one it was asked to open — from
- * the folder picker, or `dapi open <path>`. The dashboard lists these and
- * nothing else; the disk is never searched for projects. Keyed by folder,
- * which is how main addresses a project. The id is a copy of what the
- * folder's package.json said last time — enough to find the folder a URL
- * names without reading every record's package.json — and the folder has the
- * final say (see `resolveProject` in @/projects).
+ * The projects root itself is not in here: there is one, so it lives in
+ * localStorage (see @/projects).
*/
export interface ProjectRecord {
dir: string; // Absolute path of the project folder.
@@ -71,14 +54,6 @@ export interface ProjectBundle {
}
export interface GlobalDBSchema extends idb.DBSchema {
- roots: {
- value: ProjectRoot;
- key: string;
- indexes: {
- 'by-path': string;
- 'by-last-used': string;
- };
- };
projects: {
value: ProjectRecord;
key: string;
@@ -93,16 +68,13 @@ export interface GlobalDBSchema extends idb.DBSchema {
};
}
+type UpgradeTransaction = Parameters<NonNullable<idb.OpenDBCallbacks<GlobalDBSchema>['upgrade']>>[3];
+
const DB_NAME = 'diffusion-studio-idb';
const DB_VERSION = 3;
const dbPromise = openDB<GlobalDBSchema>(DB_NAME, DB_VERSION, {
- async upgrade(db, oldVersion, _newVersion, tx) {
- if (!db.objectStoreNames.contains('roots')) {
- const store = db.createObjectStore('roots', { keyPath: 'id' });
- store.createIndex('by-path', 'path', { unique: true });
- store.createIndex('by-last-used', 'lastUsedAt');
- }
+ async upgrade(db, _oldVersion, _newVersion, tx) {
if (!db.objectStoreNames.contains('bundles')) {
db.createObjectStore('bundles', { keyPath: 'projectId' });
}
@@ -111,67 +83,37 @@ const dbPromise = openDB<GlobalDBSchema>(DB_NAME, DB_VERSION, {
store.createIndex('by-id', 'id');
store.createIndex('by-last-opened', 'lastOpenedAt');
}
- if (oldVersion > 0 && oldVersion < 3) {
- const roots = tx.objectStore('roots');
- const projects = tx.objectStore('projects');
-
- let cursor = await roots.openCursor();
- while (cursor) {
- const { kind, ...root } = cursor.value as ProjectRoot & { kind?: 'multi' | 'single' };
- if (kind === 'single') {
- await projects.put({ dir: root.path, id: '', createdAt: root.createdAt, lastOpenedAt: root.lastUsedAt });
- await cursor.delete();
- } else if (kind) {
- await cursor.update(root);
- }
- cursor = await cursor.continue();
- }
- }
+ if (db.objectStoreNames.contains('roots' as never)) await retireRoots(db, tx);
},
});
-
-/** Last segment of a path, whichever separator it uses. */
-const folderLabel = (path: string): string => path.split(/[\\/]/).filter(Boolean).pop() ?? path;
-
/**
- * Records `path` as a projects root, or marks the one already recorded for
- * it as just used — which is what makes it the active one.
+ * Versions 1 and 2 kept a `roots` store: folders the dashboard scanned for
+ * projects (`kind: 'multi'`), and single project folders registered on their
+ * own (`kind: 'single'`). Version 3 does away with it. The single ones become
+ * project records; the multi ones are dropped — the projects root lives in
+ * localStorage now, and the projects the old one held come back on record
+ * when the user picks a root again (see `adoptProjectsRoot` in @/projects).
*/
-export async function rememberProjectRoot(path: string): Promise<ProjectRoot> {
- const db = await dbPromise;
- const now = new Date().toISOString();
- const existing = await db.getFromIndex('roots', 'by-path', path);
-
- const root: ProjectRoot = existing
- ? { ...existing, name: folderLabel(path), lastUsedAt: now }
- : { id: nanoid(), path, name: folderLabel(path), createdAt: now, lastUsedAt: now };
-
- await db.put('roots', root);
- return root;
-}
+async function retireRoots(db: idb.IDBPDatabase<GlobalDBSchema>, tx: UpgradeTransaction): Promise<void> {
+ type LegacyRoot = { path: string; kind?: 'multi' | 'single'; createdAt: string; lastUsedAt: string };
+ const projects = tx.objectStore('projects');
+
+ // The store is not in the schema any more, so it is addressed by name.
+ const roots = (tx as unknown as idb.IDBPTransaction<unknown, string[], 'versionchange'>).objectStore('roots');
+ for (const root of (await roots.getAll()) as LegacyRoot[]) {
+ if (root.kind !== 'single') continue;
+ await projects.put({ dir: root.path, id: '', createdAt: root.createdAt, lastOpenedAt: root.lastUsedAt });
+ }
-/** Every projects root, most recently used first. */
-export async function listProjectRoots(): Promise<ProjectRoot[]> {
- const db = await dbPromise;
- return (await db.getAllFromIndex('roots', 'by-last-used')).reverse();
+ db.deleteObjectStore('roots' as never);
}
-/** The root the app is working against: the one used last, or null when there is none. */
-export async function lastUsedProjectRoot(): Promise<ProjectRoot | null> {
- const db = await dbPromise;
- const cursor = await db
- .transaction('roots', 'readonly')
- .store.index('by-last-used')
- .openCursor(null, 'prev');
- return cursor?.value ?? null;
-}
+/** Last segment of a path, whichever separator it uses. */
+const folderLabel = (path: string): string => path.split(/[\\/]/).filter(Boolean).pop() ?? path;
-/** Forgets a projects root. The folder itself is left alone. */
-export async function forgetProjectRoot(id: string): Promise<void> {
- const db = await dbPromise;
- await db.delete('roots', id);
-}
+// ---------------------------------------------------------------------------
+// Project records
/**
* Puts the project in `dir` on record, or marks the one already recorded
@@ -191,6 +133,27 @@ export async function rememberProject(dir: string, id: string): Promise<ProjectR
return record;
}
+/**
+ * Puts every project in `found` that is not on record yet on it, leaving the
+ * ones that are alone — they were opened when they were opened. Answers with
+ * how many were new. For the projects a freshly chosen root turns out to hold.
+ */
+export async function addProjectRecords(found: Array<{ dir: string; id: string }>): Promise<number> {
+ const db = await dbPromise;
+ const now = new Date().toISOString();
+ const tx = db.transaction('projects', 'readwrite');
+ let added = 0;
+
+ for (const { dir, id } of found) {
+ if (await tx.store.getKey(dir)) continue;
+ await tx.store.put({ dir, id, createdAt: now, lastOpenedAt: now });
+ added++;
+ }
+
+ await tx.done;
+ return added;
+}
+
/** Every project on record, most recently opened first. */
export async function listProjectRecords(): Promise<ProjectRecord[]> {
const db = await dbPromise;
@@ -233,6 +196,9 @@ export async function forgetProject(dir: string): Promise<void> {
await db.delete('projects', dir);
}
+// ---------------------------------------------------------------------------
+// Project bundles
+
/** Records the bundle `projectId` just mounted, replacing the one before it. */
export async function rememberProjectBundle(projectId: string, code: string): Promise<void> {
try {
diff --git a/apps/web/src/projects/host.ts b/apps/web/src/projects/host.ts
index 7e30a161..a2d1a31e 100644
--- a/apps/web/src/projects/host.ts
+++ b/apps/web/src/projects/host.ts
@@ -8,36 +8,39 @@ import { createSignal } from 'solid-js';
import { MAIN_CHANNELS } from '@desktop/main-channels';
import { mainBridge } from '@/lib/ipc';
import {
+ addProjectRecords,
findProjectRecords,
forgetProject,
- lastUsedProjectRoot,
listProjectRecords,
moveProjectRecord,
rememberProject,
- rememberProjectRoot,
} from '@/lib/db';
import type { CompileResult, ProjectInfo, SourceEdit, WriteResult } from '@desktop/main-channels';
export type { CompileResult, ProjectInfo, SourceEdit, WriteResult };
-// The roots live in the app's IndexedDB (see @/lib/db) as a list keyed by
-// path. The app works against one of them — the one used last — but the
-// store is already the list several roots will need, so growing into them is
-// UI rather than a migration.
-//
-// Reading a database is asynchronous, so the root starts null and arrives a
-// tick later. Every call here waits for it, leaving only the UI to tell "no
-// root yet" from "no root picked" — which is what `rootsReady` is for.
+// There is one projects root, so it is one localStorage value rather than a
+// database row — read synchronously, so it is known from the first render.
+// Unset for anyone who has never picked one, and for everyone upgrading from
+// when the roots lived in the database: they get the default, or pick, the
+// next time a project is created, and the projects the old root held come
+// back with it (see `adoptProjectsRoot`).
-const [projectsRoot, setProjectsRoot] = createSignal<string | null>(null);
-const [rootsReady, setRootsReady] = createSignal(false);
+const ROOT_STORAGE_KEY = 'diffusion-studio:projects-root';
-/** The folder new projects are created in: null until one is picked, and until `rootsReady`. */
-export { projectsRoot };
+const storedRoot = (): string | null => {
+ try {
+ return window.localStorage.getItem(ROOT_STORAGE_KEY);
+ } catch {
+ return null;
+ }
+};
-/** Whether the roots have been read back from the database yet. */
-export { rootsReady };
+const [projectsRoot, setProjectsRoot] = createSignal<string | null>(storedRoot());
+
+/** The folder new projects are created in: null until one is picked. */
+export { projectsRoot };
// Bumped whenever the list of known projects changes — one created, opened,
// renamed, copied, or deleted — so a view listing them can refetch on it.
@@ -48,35 +51,49 @@ export { projectsRevision };
export const isDesktop = (): boolean => !!window.desktop;
-const ready = new Promise<void>((resolve) => {
- lastUsedProjectRoot()
- .then((root) => setProjectsRoot(root?.path ?? null))
- .catch((error) => console.error('[projects] could not read the projects database', error))
- .finally(() => {
- setRootsReady(true);
- resolve();
- });
-});
-
/** Puts `project` on the list (or marks it just opened) and tells the views. */
async function remember(project: ProjectInfo): Promise<void> {
await rememberProject(project.dir, project.id);
setProjectsRevision((revision) => revision + 1);
}
-/** The projects root, waited for: null off the desktop and until one is picked. */
+/**
+ * Makes `root` the projects root, and puts the projects it already holds on
+ * the list — the ones that are not on it yet; the rest were opened when they
+ * were opened. This is the one time the disk is searched for projects, and
+ * what brings a user's projects back after an upgrade that forgot the root,
+ * or a reinstall: pick the folder again, and there they are.
+ */
+async function adoptProjectsRoot(root: string): Promise<void> {
+ try {
+ window.localStorage.setItem(ROOT_STORAGE_KEY, root);
+ } catch (error) {
+ console.warn('[projects] could not store the projects root', error);
+ }
+ setProjectsRoot(root);
+
+ let found: ProjectInfo[] = [];
+ try {
+ found = await mainBridge.call(MAIN_CHANNELS.PROJECTS_SCAN, { root });
+ } catch (error) {
+ console.warn(`[projects] could not look for projects in ${root}`, error);
+ }
+ if (found.length && (await addProjectRecords(found))) {
+ setProjectsRevision((revision) => revision + 1);
+ }
+}
+
+/** The projects root: null off the desktop and until one is picked. */
export async function getProjectsRoot(): Promise<string | null> {
- await ready;
return projectsRoot();
}
-/** Opens the native folder picker and remembers the chosen root. */
+/** Opens the native folder picker and adopts the chosen root. */
export async function pickProjectsRoot(): Promise<string | null> {
const root = await mainBridge.call(MAIN_CHANNELS.PROJECTS_PICK_ROOT, undefined);
if (!root) return null;
- await rememberProjectRoot(root);
- setProjectsRoot(root);
+ await adoptProjectsRoot(root);
return root;
}
@@ -91,13 +108,12 @@ export async function pickProjectFolder(): Promise<string | null> {
}
/**
- * The root to work against, waited for and — when there is none to wait for —
- * defaulted to. Null off the desktop, where there is no folder at all, and
- * when the user is asked where to put projects and declines to say.
+ * The root to work against, defaulted to when there is none. Null off the
+ * desktop, where there is no folder at all, and when the user is asked where
+ * to put projects and declines to say.
*/
export async function ensureProjectsRoot(): Promise<string | null> {
if (!isDesktop()) return null;
- await ready;
const current = projectsRoot();
if (current) return current;
@@ -108,8 +124,7 @@ export async function ensureProjectsRoot(): Promise<string | null> {
const root = await mainBridge.call(MAIN_CHANNELS.PROJECTS_DEFAULT_ROOT, undefined);
if (!root) return pickProjectsRoot();
- await rememberProjectRoot(root);
- setProjectsRoot(root);
+ await adoptProjectsRoot(root);
return root;
}
@@ -119,7 +134,6 @@ export async function ensureProjectsRoot(): Promise<string | null> {
* that is not mounted) is skipped, not forgotten: it may well come back.
*/
export async function listProjects(): Promise<ProjectInfo[]> {
- await ready;
if (!isDesktop()) return [];
const records = await listProjectRecords();
@@ -129,7 +143,6 @@ export async function listProjects(): Promise<ProjectInfo[]> {
/** Creates a project folder under the root, named after `displayName`, and puts it on the list. */
export async function createProject(displayName: string): Promise<ProjectInfo> {
- await ready;
const root = projectsRoot();
if (!root) throw new Error('No projects folder selected.');
@@ -147,7 +160,6 @@ export async function createProject(displayName: string): Promise<ProjectInfo> {
* here, so the app can put an id in the URL).
*/
export async function resolveProject(ref: string): Promise<ProjectInfo | null> {
- await ready;
if (!ref || !isDesktop()) return null;
for (const record of await findProjectRecords(ref)) {
@@ -168,7 +180,6 @@ export async function resolveProject(ref: string): Promise<ProjectInfo | null> {
* `dapi open <path>` lands anywhere on disk.
*/
export async function openProjectFolder(dir: string): Promise<ProjectInfo> {
- await ready;
if (!isDesktop()) throw new Error('Opening a project folder requires the desktop app.');
const project = await mainBridge.call(MAIN_CHANNELS.PROJECTS_INIT, { dir });