Skip to content

Commit afbb103

Browse files
authored
Fix cache path resolution to match prek's own behavior (#102)
## Summary `restorePrekCache` used hardcoded platform paths (`~/.cache/prek` or `LOCALAPPDATA/prek`) to save and restore the prek hook cache. A `prek cache dir` CLI probe already existed in `prek.ts`, but it was only used by `showVerboseLogs()` to locate `prek.log`. This change makes the CLI probe the source of truth for cache save/restore as well, and fixes the fallback to match prek's actual resolution order. ## Problems - Cache path diverged from prek's actual cache location. `getCachePaths()` in `cache.ts` hardcoded `~/.cache/prek` (Linux/macOS) and `LOCALAPPDATA/prek` (Windows). prek itself resolves its cache via `PREK_HOME`, then `XDG_CACHE_HOME`, then platform defaults. Any user with `PREK_HOME` or a non-default `XDG_CACHE_HOME` would save/restore against the wrong directory. - CLI probe result was ignored for caching. `getPrekCacheDir()` already queried `prek cache dir --no-log-file` (the `--no-log-file` flag prevents the probe itself from writing to prek's trace log). But `restorePrekCache` never called it, using the hardcoded paths instead. - Duplicated, circular logic. `getCachePaths` lived in `cache.ts`, `getPrekCacheDir` lived in `prek.ts`, and they imported each other for their respective fallbacks. - `os.arch()` vs `process.arch` inconsistency. The cache key used `os.arch()` while the rest of the codebase uses `process.arch`. They return the same value, but `process.arch` is the standard convention in Node and avoids the `os` import. ## Fixes - `restorePrekCache` now calls `getPrekCacheDir()`, which probes `prek cache dir --no-log-file` first. - Removes `getCachePaths()` entirely. The fallback logic is consolidated into `getDefaultPrekCacheDir()` in `prek.ts`, mirroring prek's own resolution: `PREK_HOME` (used as-is with tilde expansion, no `/prek` suffix), then `LOCALAPPDATA/prek` on Windows, then `XDG_CACHE_HOME/prek`, then `~/.cache/prek`. - Replaces `os.arch()` with `process.arch` in the cache key. ## Future The fallback exists because `prek cache dir` was added in v0.2.2 and `--no-log-file` in v0.2.3, so the CLI probe fails on older versions still in the version manifest (back to v0.0.23). Once pre-v0.2.2 versions are dropped from the manifest, `getDefaultPrekCacheDir` can be removed entirely. ## Test plan - [x] Unit tests for `getPrekCacheDir`: CLI probe happy path, `PREK_HOME` fallback, `XDG_CACHE_HOME`/`LOCALAPPDATA` fallback
1 parent 02ce158 commit afbb103

6 files changed

Lines changed: 441 additions & 44 deletions

File tree

dist/index.js

Lines changed: 39 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post/index.js

Lines changed: 214 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)