From 202b61ec2e4e1f0b7cea83bde6bc66d444992523 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 30 Jul 2025 21:46:31 -0500 Subject: [PATCH] Get rid of project.json files --- src/components/NostrRepos.tsx | 15 +--- src/lib/ProjectsManager.test.ts | 48 ++---------- src/lib/ProjectsManager.ts | 128 ++++++++------------------------ 3 files changed, 37 insertions(+), 154 deletions(-) diff --git a/src/components/NostrRepos.tsx b/src/components/NostrRepos.tsx index cb7066a8..c23a982c 100644 --- a/src/components/NostrRepos.tsx +++ b/src/components/NostrRepos.tsx @@ -71,20 +71,7 @@ export function NostrRepos({ className, excludeProjectIds = [], onProjectCloned value: naddr, }); - // Create project metadata - const now = new Date(); - const metadata = { - name: repo.name, - createdAt: now.toISOString(), - lastModified: now.toISOString(), - }; - - // Save project metadata in .git directory - const gitDir = `${projectPath}/.git`; - await projectsManager.fs.writeFile( - `${gitDir}/project.json`, - JSON.stringify(metadata, null, 2) - ); + // No need to create project metadata - using filesystem stats and basename toast({ title: "Repository cloned", diff --git a/src/lib/ProjectsManager.test.ts b/src/lib/ProjectsManager.test.ts index e9bb9ec8..6bec23e4 100644 --- a/src/lib/ProjectsManager.test.ts +++ b/src/lib/ProjectsManager.test.ts @@ -96,29 +96,11 @@ describe('ProjectsManager', () => { projectsManager = new ProjectsManager(fs as unknown as import('./JSRuntime').JSRuntimeFS); }); - describe('getProjects with fallback detection', () => { - it('should detect projects with metadata files', async () => { + describe('getProjects', () => { + it('should detect any directory as a project', async () => { await projectsManager.init(); - // Create a project with metadata - await fs.mkdir('/projects/test-project'); - await fs.mkdir('/projects/test-project/.git'); - await fs.writeFile('/projects/test-project/.git/project.json', JSON.stringify({ - name: 'Test Project', - createdAt: '2024-01-01T00:00:00.000Z', - lastModified: '2024-01-01T00:00:00.000Z', - })); - - const projects = await projectsManager.getProjects(); - expect(projects).toHaveLength(1); - expect(projects[0].name).toBe('Test Project'); - expect(projects[0].id).toBe('test-project'); - }); - - it('should detect any directory as a project without metadata files', async () => { - await projectsManager.init(); - - // Create any directory without metadata + // Create any directory await fs.mkdir('/projects/cloned-repo'); const projects = await projectsManager.getProjects(); @@ -165,33 +147,17 @@ describe('ProjectsManager', () => { }); }); - describe('getProject with fallback detection', () => { - it('should get project with metadata file', async () => { + describe('getProject', () => { + it('should get any directory as a project', async () => { await projectsManager.init(); - // Create a project with metadata - await fs.mkdir('/projects/test-project'); - await fs.mkdir('/projects/test-project/.git'); - await fs.writeFile('/projects/test-project/.git/project.json', JSON.stringify({ - name: 'Test Project', - createdAt: '2024-01-01T00:00:00.000Z', - lastModified: '2024-01-01T00:00:00.000Z', - })); - - const project = await projectsManager.getProject('test-project'); - expect(project).not.toBeNull(); - expect(project?.name).toBe('Test Project'); - }); - - it('should get any directory as a project without metadata file', async () => { - await projectsManager.init(); - - // Create any directory without metadata + // Create any directory await fs.mkdir('/projects/cloned-repo'); const project = await projectsManager.getProject('cloned-repo'); expect(project).not.toBeNull(); expect(project?.name).toBe('Cloned Repo'); + expect(project?.id).toBe('cloned-repo'); }); it('should return null for non-existent projects', async () => { diff --git a/src/lib/ProjectsManager.ts b/src/lib/ProjectsManager.ts index dd712f2e..bca3e7c9 100644 --- a/src/lib/ProjectsManager.ts +++ b/src/lib/ProjectsManager.ts @@ -19,12 +19,6 @@ export interface Project { lastModified: Date; } -interface ProjectMetadata { - name: string; - createdAt: string; - lastModified: string; -} - export class ProjectsManager { fs: JSRuntimeFS; dir: string; @@ -48,30 +42,20 @@ export class ProjectsManager { await this.fs.mkdir(projectPath); - const now = new Date(); - const metadata: ProjectMetadata = { - name, - createdAt: now.toISOString(), - lastModified: now.toISOString(), - }; - // Clone the template first await this.cloneTemplate(projectPath); - // Save project metadata in .git directory to keep it out of source tree - const gitDir = `${projectPath}/.git`; - await this.fs.writeFile( - `${gitDir}/project.json`, - JSON.stringify(metadata, null, 2) - ); + // Get filesystem stats for timestamps + const stats = await this.fs.stat(projectPath); + const timestamp = stats.mtimeMs ? new Date(stats.mtimeMs) : new Date(); // Return the full project object with dynamically generated properties return { id, - name, + name: this.formatProjectName(id), // Use formatted directory name path: projectPath, - createdAt: now, - lastModified: now, + createdAt: timestamp, + lastModified: timestamp, }; } @@ -93,46 +77,27 @@ export class ProjectsManager { for (const dir of projectDirs) { const projectPath = `${this.dir}/${dir}`; - const projectFile = `${projectPath}/.git/project.json`; try { - // Try to read the project metadata file first - const projectData = await this.fs.readFile(projectFile, 'utf8'); - const metadata: ProjectMetadata = JSON.parse(projectData); + const stats = await this.fs.stat(projectPath); + if (stats.isDirectory()) { + const modifiedDate = stats.mtimeMs ? new Date(stats.mtimeMs) : new Date(); + const project: Project = { + id: dir, // basename of the path + name: this.formatProjectName(dir), // Convert directory name to readable format + path: projectPath, + createdAt: modifiedDate, // Use modified time as creation time + lastModified: modifiedDate, + }; - // Generate dynamic properties - const project: Project = { - id: dir, // basename of the path - name: metadata.name, - path: projectPath, - createdAt: new Date(metadata.createdAt), - lastModified: new Date(metadata.lastModified), - }; - - projects.push(project); - } catch { - // If metadata file doesn't exist, treat any directory as a project - try { - const stats = await this.fs.stat(projectPath); - if (stats.isDirectory()) { - const modifiedDate = stats.mtimeMs ? new Date(stats.mtimeMs) : new Date(); - const fallbackProject: Project = { - id: dir, - name: this.formatProjectName(dir), // Convert directory name to readable format - path: projectPath, - createdAt: modifiedDate, // Use modified time as creation time fallback - lastModified: modifiedDate, - }; - - projects.push(fallbackProject); - } - } catch { - // Skip if we can't stat the directory + projects.push(project); } + } catch { + // Skip if we can't stat the directory } } - return projects.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); + return projects.sort((a, b) => b.lastModified.getTime() - a.lastModified.getTime()); } catch { return []; } @@ -141,41 +106,20 @@ export class ProjectsManager { async getProject(id: string): Promise { try { const projectPath = `${this.dir}/${id}`; - const projectFile = `${projectPath}/.git/project.json`; + const stats = await this.fs.stat(projectPath); - try { - // Try to read the project metadata file first - const projectData = await this.fs.readFile(projectFile, 'utf8'); - const metadata: ProjectMetadata = JSON.parse(projectData); - - // Generate dynamic properties + if (stats.isDirectory()) { + const modifiedDate = stats.mtimeMs ? new Date(stats.mtimeMs) : new Date(); return { id, // basename of the path - name: metadata.name, + name: this.formatProjectName(id), // Convert directory name to readable format path: projectPath, - createdAt: new Date(metadata.createdAt), - lastModified: new Date(metadata.lastModified), + createdAt: modifiedDate, // Use modified time as creation time + lastModified: modifiedDate, }; - } catch { - // If metadata file doesn't exist, treat any directory as a project - try { - const stats = await this.fs.stat(projectPath); - if (stats.isDirectory()) { - const modifiedDate = stats.mtimeMs ? new Date(stats.mtimeMs) : new Date(); - return { - id, - name: this.formatProjectName(id), // Convert directory name to readable format - path: projectPath, - createdAt: modifiedDate, // Use modified time as creation time fallback - lastModified: modifiedDate, - }; - } - } catch { - // Directory doesn't exist - } - - return null; } + + return null; } catch { return null; } @@ -204,15 +148,11 @@ export class ProjectsManager { const dir = fullPath.split('/').slice(0, -1).join('/'); await this.fs.mkdir(dir); await this.fs.writeFile(fullPath, content); - - // Update last modified - await this.updateProjectLastModified(projectId); } async deleteFile(projectId: string, filePath: string): Promise { const fullPath = `${this.dir}/${projectId}/${filePath}`; await this.fs.unlink(fullPath); - await this.updateProjectLastModified(projectId); } async listFiles(projectId: string, dirPath: string = ''): Promise { @@ -322,17 +262,7 @@ export class ProjectsManager { return naddr !== null; } - private async updateProjectLastModified(projectId: string): Promise { - try { - const projectFile = `${this.dir}/${projectId}/.git/project.json`; - const projectData = await this.fs.readFile(projectFile, 'utf8'); - const metadata: ProjectMetadata = JSON.parse(projectData); - metadata.lastModified = new Date().toISOString(); - await this.fs.writeFile(projectFile, JSON.stringify(metadata, null, 2)); - } catch { - // Ignore errors updating last modified - } - } +