()
| 26 | * Returns empty array if file doesn't exist or is invalid |
| 27 | */ |
| 28 | export async function loadHistory(): Promise<string[]> { |
| 29 | const filePath = getHistoryFilePath() |
| 30 | |
| 31 | try { |
| 32 | const content = await fs.readFile(filePath, "utf-8") |
| 33 | const data: HistoryData = JSON.parse(content) |
| 34 | |
| 35 | // Validate structure |
| 36 | if (!data || typeof data !== "object") { |
| 37 | return [] |
| 38 | } |
| 39 | |
| 40 | if (!Array.isArray(data.entries)) { |
| 41 | return [] |
| 42 | } |
| 43 | |
| 44 | // Filter to only valid strings |
| 45 | return data.entries.filter((entry): entry is string => typeof entry === "string" && entry.trim().length > 0) |
| 46 | } catch (err) { |
| 47 | const error = err as NodeJS.ErrnoException |
| 48 | // File doesn't exist - that's expected on first run |
| 49 | if (error.code === "ENOENT") { |
| 50 | return [] |
| 51 | } |
| 52 | |
| 53 | // JSON parse error or other issue - log and return empty |
| 54 | console.error("Warning: Could not load CLI history:", error.message) |
| 55 | return [] |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Save history entries to file |
no test coverage detected