| 1 | import fs from 'fs' |
| 2 | |
| 3 | export type PageHtmlSnapshot = { |
| 4 | restore: () => Promise<void> |
| 5 | } |
| 6 | |
| 7 | export const capturePageHtmlSnapshot = async (htmlPath: string): Promise<PageHtmlSnapshot> => { |
| 8 | try { |
| 9 | const html = await fs.promises.readFile(htmlPath, 'utf-8') |
| 10 | return { |
| 11 | restore: async () => { |
| 12 | await fs.promises.writeFile(htmlPath, html, 'utf-8') |
| 13 | } |
| 14 | } |
| 15 | } catch (error) { |
| 16 | if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error |
| 17 | return { |
| 18 | restore: async () => { |
| 19 | await fs.promises.rm(htmlPath, { force: true }) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 |