(targetPath: string, data: string)
| 13 | * relying on each call site to remember an ensureDir. |
| 14 | */ |
| 15 | export function writeFileAtomic(targetPath: string, data: string): void { |
| 16 | mkdirSync(dirname(targetPath), { recursive: true }); |
| 17 | const tmpPath = `${targetPath}.tmp`; |
| 18 | writeFileSync(tmpPath, data, { encoding: "utf-8" }); |
| 19 | try { |
| 20 | if (statSync(targetPath, { throwIfNoEntry: false })?.isFile()) { |
| 21 | const mode = statSync(targetPath).mode & 0o777; |
| 22 | chmodSync(tmpPath, mode); |
| 23 | } |
| 24 | } catch { |
| 25 | // New file — default umask applies. |
| 26 | } |
| 27 | renameSync(tmpPath, targetPath); |
| 28 | } |
no outgoing calls
no test coverage detected