( filePath: string, content: string )
| 97 | } |
| 98 | |
| 99 | export async function writeFileAtomically( |
| 100 | filePath: string, |
| 101 | content: string |
| 102 | ): Promise<void> { |
| 103 | const dirPath = path.dirname(filePath); |
| 104 | await FileSystemUtils.createDirectory(dirPath); |
| 105 | const tempPath = path.join( |
| 106 | dirPath, |
| 107 | `.${path.basename(filePath)}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp` |
| 108 | ); |
| 109 | |
| 110 | try { |
| 111 | await fs.writeFile(tempPath, content, 'utf-8'); |
| 112 | await fs.rename(tempPath, filePath); |
| 113 | } catch (error) { |
| 114 | await fs.rm(tempPath, { force: true }).catch(() => undefined); |
| 115 | throw error; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | export async function acquireFileLock( |
| 120 | options: FileLockOptions |
no test coverage detected