| 1 | import fs from 'fs' |
| 2 | |
| 3 | export type PageEditFileSnapshot = { |
| 4 | path: string |
| 5 | exists: boolean |
| 6 | content: string |
| 7 | } |
| 8 | |
| 9 | export async function restorePageEditSnapshots( |
| 10 | snapshots: readonly PageEditFileSnapshot[] |
| 11 | ): Promise<Array<{ path: string; error: unknown }>> { |
| 12 | const results = await Promise.allSettled( |
| 13 | snapshots.map((snapshot) => |
| 14 | snapshot.exists |
| 15 | ? fs.promises.writeFile(snapshot.path, snapshot.content, 'utf-8') |
| 16 | : fs.promises.rm(snapshot.path, { force: true }) |
| 17 | ) |
| 18 | ) |
| 19 | |
| 20 | return results.flatMap((result, index) => |
| 21 | result.status === 'rejected' |
| 22 | ? [{ path: snapshots[index]?.path || '', error: result.reason }] |
| 23 | : [] |
| 24 | ) |
| 25 | } |
| 26 |