/** * 项目管理器 * 负责项目的创建和管理 */ import * as storage from './storage/index.js'; import { getProjectId, isGitRepository } from './project.js'; /** * 项目元数据 */ export interface ProjectMetadata { id: string; workdir: string; createdAt: string; isGitRepo: boolean; } /** * 项目管理器 */ export class ProjectManager { private currentProject: ProjectMetadata | null = null; /** * 获取当前项目 */ getProject(): ProjectMetadata | null { return this.currentProject; } /** * 设置当前项目 */ setProject(project: ProjectMetadata | null): void { this.currentProject = project; } /** * 获取或创建项目 */ async getOrCreate(workdir: string): Promise { const projectId = await getProjectId(workdir); try { const existing = await storage.read(['project', projectId]); this.currentProject = existing; return existing; } catch (e) { if (e instanceof storage.StorageNotFoundError) { const isGitRepo = await isGitRepository(workdir); const project: ProjectMetadata = { id: projectId, workdir, createdAt: new Date().toISOString(), isGitRepo, }; await storage.write(['project', projectId], project); this.currentProject = project; return project; } throw e; } } /** * 切换项目 */ async switchProject(workdir: string): Promise { return this.getOrCreate(workdir); } /** * 检查项目是否初始化 */ isInitialized(): boolean { return this.currentProject !== null; } /** * 获取项目 ID */ getProjectId(): string | null { return this.currentProject?.id ?? null; } }