()
| 64 | } |
| 65 | |
| 66 | async pruneOrphans(): Promise<string[]> { |
| 67 | const removed: string[] = []; |
| 68 | const index = await this.readIndex(); |
| 69 | let indexChanged = false; |
| 70 | |
| 71 | for (const key of Object.keys(index)) { |
| 72 | if (!this.usedKeys.has(key)) { |
| 73 | const file = path.join(this.cacheDir, `${key}.txt`); |
| 74 | await unlink(file).catch(() => undefined); |
| 75 | removed.push(index[key] ?? key); |
| 76 | delete index[key]; |
| 77 | indexChanged = true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (indexChanged) { |
| 82 | this.indexCache = index; |
| 83 | await writeFile( |
| 84 | this.indexFile(), |
| 85 | `${JSON.stringify(sortKeys(index), null, 2)}\n`, |
| 86 | "utf8" |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | const expectedKeys = new Set(Object.keys(index)); |
| 91 | const expectedKeys2 = new Set([...expectedKeys].map((k) => `${k}.txt`)); |
| 92 | expectedKeys2.add("_index.json"); |
| 93 | try { |
| 94 | const entries = await readdir(this.cacheDir); |
| 95 | for (const entry of entries) { |
| 96 | if (!expectedKeys2.has(entry)) { |
| 97 | await unlink(path.join(this.cacheDir, entry)).catch(() => undefined); |
| 98 | removed.push(entry); |
| 99 | } |
| 100 | } |
| 101 | } catch { |
| 102 | // cache dir may not exist yet |
| 103 | } |
| 104 | |
| 105 | return removed; |
| 106 | } |
| 107 | |
| 108 | getWarnings(): CacheWarning[] { |
| 109 | return [...this.warnings]; |
no test coverage detected