()
| 23 | * @returns Array of recent projects, sorted by most recent first |
| 24 | */ |
| 25 | export const loadRecentProjects = (): RecentProject[] => { |
| 26 | const recentProjectsPath = getRecentProjectsPath() |
| 27 | |
| 28 | if (!fs.existsSync(recentProjectsPath)) { |
| 29 | return [] |
| 30 | } |
| 31 | |
| 32 | try { |
| 33 | const fileContent = fs.readFileSync(recentProjectsPath, 'utf8') |
| 34 | const parsed = JSON.parse(fileContent) |
| 35 | |
| 36 | if (!Array.isArray(parsed)) { |
| 37 | return [] |
| 38 | } |
| 39 | |
| 40 | // Validate and filter entries |
| 41 | const validProjects = parsed.filter( |
| 42 | (item): item is RecentProject => |
| 43 | typeof item === 'object' && |
| 44 | item !== null && |
| 45 | typeof item.path === 'string' && |
| 46 | typeof item.lastOpened === 'number', |
| 47 | ) |
| 48 | |
| 49 | // Filter out projects that no longer exist on disk |
| 50 | const existingProjects = validProjects.filter((project) => { |
| 51 | try { |
| 52 | return fs.existsSync(project.path) |
| 53 | } catch { |
| 54 | return false |
| 55 | } |
| 56 | }) |
| 57 | |
| 58 | // Sort by most recent first |
| 59 | return existingProjects.sort((a, b) => b.lastOpened - a.lastOpened) |
| 60 | } catch (error) { |
| 61 | logger.debug( |
| 62 | { error: error instanceof Error ? error.message : String(error) }, |
| 63 | 'Error reading recent projects', |
| 64 | ) |
| 65 | return [] |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Clear all recent projects |
no test coverage detected