| 1 | import type { ResolvedSlidevOptions } from '@slidev/types' |
| 2 | import { existsSync } from 'node:fs' |
| 3 | import fs from 'node:fs/promises' |
| 4 | import { dirname, join, resolve } from 'pathe' |
| 5 | |
| 6 | function resolveSnapshotsDir(options: ResolvedSlidevOptions): string { |
| 7 | return resolve(dirname(options.entry), '.slidev/snapshots') |
| 8 | } |
| 9 | |
| 10 | export async function loadSnapshots(options: ResolvedSlidevOptions) { |
| 11 | const dir = resolveSnapshotsDir(options) |
| 12 | const file = join(dir, 'snapshots.json') |
| 13 | if (!dir || !existsSync(file)) |
| 14 | return {} |
| 15 | |
| 16 | return JSON.parse(await fs.readFile(file, 'utf8')) |
| 17 | } |
| 18 | |
| 19 | export async function writeSnapshots(options: ResolvedSlidevOptions, data: Record<string, any>) { |
| 20 | const dir = resolveSnapshotsDir(options) |
| 21 | if (!dir) |
| 22 | return |
| 23 | |
| 24 | await fs.mkdir(dir, { recursive: true }) |
| 25 | // TODO: write as each image file |
| 26 | await fs.writeFile(join(dir, 'snapshots.json'), JSON.stringify(data, null, 2), 'utf-8') |
| 27 | } |
| 28 |