(filePath: string)
| 56 | * existing config that happened to break JSON parse temporarily. |
| 57 | */ |
| 58 | export function readJsonFile(filePath: string): Record<string, any> { |
| 59 | if (!fs.existsSync(filePath)) { |
| 60 | return {}; |
| 61 | } |
| 62 | try { |
| 63 | return JSON.parse(fs.readFileSync(filePath, 'utf-8')); |
| 64 | } catch (err) { |
| 65 | const msg = err instanceof Error ? err.message : String(err); |
| 66 | console.warn(` Warning: Could not parse ${path.basename(filePath)}: ${msg}`); |
| 67 | console.warn(` A backup will be created before overwriting.`); |
| 68 | try { |
| 69 | fs.copyFileSync(filePath, filePath + '.backup'); |
| 70 | } catch { /* ignore backup failure */ } |
| 71 | return {}; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Write a file atomically: write to `<path>.tmp.<pid>`, then rename. |
no test coverage detected