| 150 | static maxVersions = 4 |
| 151 | |
| 152 | constructor(cwd: string) { |
| 153 | const version = fingerprintOfCode() |
| 154 | const resolvedCwd = resolve(cwd) |
| 155 | const project = projectOf(resolvedCwd) |
| 156 | const base = |
| 157 | existsSync(join(project, "package.json")) ? project : resolvedCwd |
| 158 | const root = join(base, "node_modules", ".cache", "ark") |
| 159 | this.dir = join(root, version) |
| 160 | mkdirSync(this.dir, { recursive: true }) |
| 161 | const siblings = readdirSync(root).filter( |
| 162 | name => name !== version && isHash(name) |
| 163 | ) |
| 164 | const maxSiblings = Math.max(0, AnswerCache.maxVersions - 1) |
| 165 | if (siblings.length > maxSiblings) { |
| 166 | const paths = siblings.map(name => join(root, name)) |
| 167 | const mtimes = Object.fromEntries( |
| 168 | paths.map(path => [path, mtimeOf(path)]) |
| 169 | ) |
| 170 | paths.sort((l, r) => mtimes[l] - mtimes[r]) |
| 171 | for (const path of paths.slice(0, paths.length - maxSiblings)) |
| 172 | rmSync(path, { recursive: true, force: true }) |
| 173 | } |
| 174 | const names = readdirSync(this.dir) |
| 175 | if (names.length > AnswerCache.maxEntries) { |
| 176 | const paths = names.map(name => join(this.dir, name)) |
| 177 | const mtimes = Object.fromEntries( |
| 178 | paths.map(path => [path, mtimeOf(path)]) |
| 179 | ) |
| 180 | paths.sort((l, r) => mtimes[l] - mtimes[r]) |
| 181 | for (const path of paths.slice( |
| 182 | 0, |
| 183 | paths.length - AnswerCache.maxEntries / 2 |
| 184 | )) |
| 185 | rmSync(path, { force: true }) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // an answer stored for the key whose snapshot still holds |
| 190 | get(key: string, holds: (snapshot: string) => boolean): unknown { |