silverwolf-x/project / packages /next /src /lib /fs /write-atomic.ts
silverwolf-x's picture
download
raw
433 Bytes
import { unlinkSync, writeFileSync } from 'fs'
import { renameSync } from './rename'
export function writeFileAtomic(filePath: string, content: string): void {
const tempPath = filePath + '.tmp.' + Math.random().toString(36).slice(2)
try {
writeFileSync(tempPath, content, 'utf-8')
renameSync(tempPath, filePath)
} catch (e) {
try {
unlinkSync(tempPath)
} catch {
// ignore
}
throw e
}
}

Xet Storage Details

Size:
433 Bytes
·
Xet hash:
a569ad90ee4f76d0fe943b7d50abeb0b985d8dd92be78d5b9d092e272b997e8f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.