| 15 | } |
| 16 | |
| 17 | export async function cleanupStoreFiles(userDataPath: string, now = Date.now()) { |
| 18 | const entries = await readdir(userDataPath, { withFileTypes: true }).catch(() => []) |
| 19 | const candidates = ( |
| 20 | await Promise.all( |
| 21 | entries |
| 22 | .filter((entry) => entry.isFile()) |
| 23 | .map(async (entry) => { |
| 24 | const kind = storeKind(entry.name) |
| 25 | if (!kind) return |
| 26 | |
| 27 | const file = join(userDataPath, entry.name) |
| 28 | const stats = await stat(file).catch(() => undefined) |
| 29 | if (!stats?.isFile()) return |
| 30 | |
| 31 | return { |
| 32 | name: entry.name, |
| 33 | path: file, |
| 34 | kind, |
| 35 | modified: stats.mtimeMs, |
| 36 | empty: await isEmptyStore(file, stats.size), |
| 37 | } |
| 38 | }), |
| 39 | ) |
| 40 | ).filter((candidate) => !!candidate) |
| 41 | |
| 42 | const stale = new Set<StoreCandidate>() |
| 43 | for (const candidate of candidates) { |
| 44 | if (candidate.empty) stale.add(candidate) |
| 45 | if (candidate.kind === "draft" && now - candidate.modified > DRAFT_RETENTION_MS) stale.add(candidate) |
| 46 | } |
| 47 | |
| 48 | candidates |
| 49 | .filter((candidate) => candidate.kind === "draft" && !candidate.empty) |
| 50 | .sort((a, b) => b.modified - a.modified) |
| 51 | .slice(DRAFT_KEEP_RECENT) |
| 52 | .forEach((candidate) => stale.add(candidate)) |
| 53 | |
| 54 | const deleted = await Promise.all( |
| 55 | [...stale].map(async (candidate) => { |
| 56 | await rm(candidate.path, { force: true }) |
| 57 | return candidate.name |
| 58 | }), |
| 59 | ) |
| 60 | |
| 61 | return { scanned: candidates.length, deleted } |
| 62 | } |
| 63 | |
| 64 | export async function deleteStoreFileIfEmpty(userDataPath: string, name: string) { |
| 65 | if (!storeKind(name)) return false |