(filePath: string, content: string)
| 79 | * file is cleaned up on rename failure. |
| 80 | */ |
| 81 | export function atomicWriteFileSync(filePath: string, content: string): void { |
| 82 | const dir = path.dirname(filePath); |
| 83 | if (!fs.existsSync(dir)) { |
| 84 | fs.mkdirSync(dir, { recursive: true }); |
| 85 | } |
| 86 | const tmpPath = filePath + '.tmp.' + process.pid; |
| 87 | try { |
| 88 | fs.writeFileSync(tmpPath, content); |
| 89 | fs.renameSync(tmpPath, filePath); |
| 90 | } catch (err) { |
| 91 | try { fs.unlinkSync(tmpPath); } catch { /* ignore */ } |
| 92 | throw err; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Atomic JSON write. Trailing newline matches the convention every |
no outgoing calls
no test coverage detected