(projectPath: string)
| 115 | * Validates that the path exists before saving. |
| 116 | */ |
| 117 | export const saveRecentProject = (projectPath: string): void => { |
| 118 | // Validate path exists before saving |
| 119 | if (!fs.existsSync(projectPath)) { |
| 120 | logger.debug({ projectPath }, 'Skipping save for non-existent project path') |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | const configDir = getConfigDir() |
| 125 | const recentProjectsPath = getRecentProjectsPath() |
| 126 | |
| 127 | try { |
| 128 | if (!fs.existsSync(configDir)) { |
| 129 | fs.mkdirSync(configDir, { recursive: true }) |
| 130 | } |
| 131 | |
| 132 | // Load existing projects |
| 133 | const existingProjects = loadRecentProjects() |
| 134 | |
| 135 | // Remove the project if it already exists (we'll add it back at the top) |
| 136 | const filteredProjects = existingProjects.filter( |
| 137 | (p) => p.path !== projectPath, |
| 138 | ) |
| 139 | |
| 140 | // Add the new/updated project at the beginning |
| 141 | const updatedProjects: RecentProject[] = [ |
| 142 | { path: projectPath, lastOpened: Date.now() }, |
| 143 | ...filteredProjects, |
| 144 | ].slice(0, MAX_RECENT_PROJECTS) |
| 145 | |
| 146 | fs.writeFileSync( |
| 147 | recentProjectsPath, |
| 148 | JSON.stringify(updatedProjects, null, 2), |
| 149 | ) |
| 150 | } catch (error) { |
| 151 | logger.debug( |
| 152 | { error: error instanceof Error ? error.message : String(error) }, |
| 153 | 'Error saving recent project', |
| 154 | ) |
| 155 | } |
| 156 | } |
no test coverage detected