File size: 457 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import { unlink, writeFile } from 'fs/promises'
import { rename } from './rename'
export async function writeFileAtomic(
filePath: string,
content: string
): Promise<void> {
const tempPath = filePath + '.tmp.' + Math.random().toString(36).slice(2)
try {
await writeFile(tempPath, content, 'utf-8')
await rename(tempPath, filePath)
} catch (e) {
try {
await unlink(tempPath)
} catch {
// ignore
}
throw e
}
}
|