(kimiHomeDir: string)
| 29 | const EMPTY: InstalledFile = { version: 1, plugins: [] }; |
| 30 | |
| 31 | export async function readInstalled(kimiHomeDir: string): Promise<InstalledFile> { |
| 32 | const filePath = path.join(kimiHomeDir, INSTALLED_REL); |
| 33 | let text: string; |
| 34 | try { |
| 35 | text = await readFile(filePath, 'utf8'); |
| 36 | } catch (error) { |
| 37 | if ((error as NodeJS.ErrnoException).code === 'ENOENT') return EMPTY; |
| 38 | throw error; |
| 39 | } |
| 40 | try { |
| 41 | const parsed = JSON.parse(text) as InstalledFile; |
| 42 | if (typeof parsed !== 'object' || parsed === null || !Array.isArray(parsed.plugins)) { |
| 43 | throw new Error('installed.json is not a valid InstalledFile object'); |
| 44 | } |
| 45 | return parsed; |
| 46 | } catch (error) { |
| 47 | throw new Error( |
| 48 | `Failed to parse ${filePath}: ${(error as Error).message}`, |
| 49 | { cause: error }, |
| 50 | ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | export async function writeInstalled( |
| 55 | kimiHomeDir: string, |
no test coverage detected