From e4cf0d333fa9a66295dda950366c603810b3d19e Mon Sep 17 00:00:00 2001 From: hugo Date: Tue, 10 Feb 2026 22:09:26 +0100 Subject: [PATCH] fix: import now does something and project folders are managed correctly --- src/main/engine/ProjectEngine.ts | 14 +++++++------- src/main/ipc/handlers.ts | 23 ++++++++++++++++++++++- tests/engine/ProjectEngine.test.ts | 16 +++++++++------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/main/engine/ProjectEngine.ts b/src/main/engine/ProjectEngine.ts index f2625ed..ed1a55c 100644 --- a/src/main/engine/ProjectEngine.ts +++ b/src/main/engine/ProjectEngine.ts @@ -29,9 +29,9 @@ export class ProjectEngine extends EventEmitter { .replace(/^-|-$/g, ''); } - private async ensureProjectDirectories(slug: string): Promise { + private async ensureProjectDirectories(projectId: string): Promise { const userDataPath = app.getPath('userData'); - const projectDir = path.join(userDataPath, 'projects', slug); + const projectDir = path.join(userDataPath, 'projects', projectId); const postsDir = path.join(projectDir, 'posts'); const mediaDir = path.join(projectDir, 'media'); @@ -66,8 +66,8 @@ export class ProjectEngine extends EventEmitter { isActive: false, }; - // Create directories - await this.ensureProjectDirectories(finalSlug); + // Create directories using project ID (not slug) + await this.ensureProjectDirectories(id); // Insert into database const dbProject: NewProject = { @@ -219,11 +219,11 @@ export class ProjectEngine extends EventEmitter { return project; } - getProjectPaths(projectSlug: string): { posts: string; media: string } { + getProjectPaths(projectId: string): { posts: string; media: string } { const userDataPath = app.getPath('userData'); return { - posts: path.join(userDataPath, 'projects', projectSlug, 'posts'), - media: path.join(userDataPath, 'projects', projectSlug, 'media'), + posts: path.join(userDataPath, 'projects', projectId, 'posts'), + media: path.join(userDataPath, 'projects', projectId, 'media'), }; } } diff --git a/src/main/ipc/handlers.ts b/src/main/ipc/handlers.ts index 31388a2..1be3b04 100644 --- a/src/main/ipc/handlers.ts +++ b/src/main/ipc/handlers.ts @@ -130,7 +130,13 @@ export function registerIpcHandlers(): void { }); ipcMain.handle('posts:rebuildFromFiles', async () => { + // Ensure project context is current before rebuilding + const projectEngine = getProjectEngine(); + const project = await projectEngine.getActiveProject(); const engine = getPostEngine(); + if (project) { + engine.setProjectContext(project.id); + } return engine.rebuildDatabaseFromFiles(); }); @@ -231,7 +237,13 @@ export function registerIpcHandlers(): void { }); ipcMain.handle('media:rebuildFromFiles', async () => { + // Ensure project context is current before rebuilding + const projectEngine = getProjectEngine(); + const project = await projectEngine.getActiveProject(); const engine = getMediaEngine(); + if (project) { + engine.setProjectContext(project.id); + } return engine.rebuildDatabaseFromFiles(); }); @@ -373,7 +385,16 @@ export function registerIpcHandlers(): void { // ============ App Handlers ============ ipcMain.handle('app:getDataPaths', async () => { - return getDatabase().getDataPaths(); + // Get paths for the active project + const projectEngine = getProjectEngine(); + const activeProject = await projectEngine.getActiveProject(); + const projectId = activeProject?.id || 'default'; + const paths = projectEngine.getProjectPaths(projectId); + return { + database: getDatabase().getDataPaths().database, + posts: paths.posts, + media: paths.media, + }; }); ipcMain.handle('app:openFolder', async (_, folderPath: string) => { diff --git a/tests/engine/ProjectEngine.test.ts b/tests/engine/ProjectEngine.test.ts index d4fa76f..acfbf9d 100644 --- a/tests/engine/ProjectEngine.test.ts +++ b/tests/engine/ProjectEngine.test.ts @@ -254,19 +254,21 @@ describe('ProjectEngine', () => { describe('getProjectPaths', () => { it('should return paths for posts and media directories', () => { - const paths = projectEngine.getProjectPaths('my-blog'); + const projectId = 'test-project-id'; + const paths = projectEngine.getProjectPaths(projectId); - expect(paths.posts).toContain('my-blog'); + expect(paths.posts).toContain(projectId); expect(paths.posts).toContain('posts'); - expect(paths.media).toContain('my-blog'); + expect(paths.media).toContain(projectId); expect(paths.media).toContain('media'); }); - it('should include project slug in paths', () => { - const paths = projectEngine.getProjectPaths('custom-project'); + it('should include project ID in paths', () => { + const projectId = 'custom-project-uuid'; + const paths = projectEngine.getProjectPaths(projectId); - expect(paths.posts).toContain('custom-project'); - expect(paths.media).toContain('custom-project'); + expect(paths.posts).toContain(projectId); + expect(paths.media).toContain(projectId); }); });