-
Notifications
You must be signed in to change notification settings - Fork 7
fix(sdk,cli): evict stale local bundle cache entries, add cache info/clear commands (#60, #78, #79, #95) #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shwetank-dev
wants to merge
6
commits into
NimbleBrainInc:main
Choose a base branch
from
shwetank-dev:issue-79-78-60-95
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a3e6ca
fix(sdk): check platform in loadBundle cache guards (#78)
shwetank-dev af58082
fix(cli): populate bundle cache after pull (#60)
shwetank-dev ca994bc
fix(sdk): surface check failures in checkForUpdate instead of swallow…
shwetank-dev 6aad77c
fix(sdk,cli): evict stale local bundle cache entries and add cache in…
shwetank-dev ac3a99a
feat(cli): add mpak cache clear command (#95)
shwetank-dev f3826c2
chore(sdk): fix prettier formatting in cache.ts, index.ts, cache.test.ts
shwetank-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { createInterface } from 'node:readline'; | ||
| import { existsSync, rmSync } from 'node:fs'; | ||
| import { mpak } from '../../utils/config.js'; | ||
| import { formatSize, logger } from '../../utils/format.js'; | ||
|
|
||
| export interface CacheClearOptions { | ||
| force?: boolean; | ||
| } | ||
|
|
||
| async function confirmPrompt(question: string): Promise<boolean> { | ||
| const rl = createInterface({ input: process.stdin, output: process.stderr }); | ||
| return new Promise((resolve) => { | ||
| rl.question(question, (answer) => { | ||
| rl.close(); | ||
| resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes'); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export async function handleCacheClear( | ||
| options: CacheClearOptions = {}, | ||
| _confirm = confirmPrompt, | ||
| ): Promise<void> { | ||
| const info = mpak.bundleCache.getCacheInfo(); | ||
| const entryCount = info.registryBundles.length + info.localBundles.length; | ||
|
|
||
| if (entryCount === 0) { | ||
| logger.info('Cache is already empty.'); | ||
| return; | ||
| } | ||
|
|
||
| const sizeStr = formatSize(info.totalBytes); | ||
| const summary = `${entryCount} bundle(s), ${sizeStr}`; | ||
|
|
||
| if (!options.force) { | ||
| const ok = await _confirm(`Clear the entire cache (${summary})? [y/N] `); | ||
| if (!ok) { | ||
| logger.info('Aborted.'); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const cacheHome = mpak.bundleCache.cacheHome; | ||
| if (existsSync(cacheHome)) { | ||
| rmSync(cacheHome, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| logger.info(`Cleared cache. Freed ${sizeStr}.`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { handleCacheInfo } from './info.js'; | ||
| export { handleCacheClear } from './clear.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { mpak } from '../../utils/config.js'; | ||
| import { formatSize, logger, table } from '../../utils/format.js'; | ||
|
|
||
| export interface CacheInfoOptions { | ||
| json?: boolean; | ||
| } | ||
|
|
||
| export async function handleCacheInfo(options: CacheInfoOptions = {}): Promise<void> { | ||
| const info = mpak.bundleCache.getCacheInfo(); | ||
|
|
||
| if (options.json) { | ||
| console.log(JSON.stringify(info, null, 2)); | ||
| return; | ||
| } | ||
|
|
||
| if (info.registryBundles.length === 0 && info.localBundles.length === 0) { | ||
| logger.info('Cache is empty.'); | ||
| return; | ||
| } | ||
|
|
||
| if (info.registryBundles.length > 0) { | ||
| logger.info('Registry bundles:\n'); | ||
| logger.info( | ||
| table( | ||
| ['Bundle', 'Version', 'Pulled', 'Size'], | ||
| info.registryBundles.map((b) => [ | ||
| b.name, | ||
| b.version, | ||
| b.pulledAt.slice(0, 10), | ||
| formatSize(b.bytes), | ||
| ]), | ||
| { rightAlign: [3] }, | ||
| ), | ||
| ); | ||
| logger.info(''); | ||
| } | ||
|
|
||
| if (info.localBundles.length > 0) { | ||
| logger.info('Local bundles:\n'); | ||
| logger.info( | ||
| table( | ||
| ['Path', 'Extracted', 'Size'], | ||
| info.localBundles.map((b) => [ | ||
| b.localPath, | ||
| b.extractedAt.slice(0, 10), | ||
| formatSize(b.bytes), | ||
| ]), | ||
| { rightAlign: [2] }, | ||
| ), | ||
| ); | ||
| logger.info(''); | ||
| } | ||
|
|
||
| logger.info(`Total: ${formatSize(info.totalBytes)}`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mock is set up but no test asserts that
extractBundleis actually invoked — the whole point of #60 is unverified at the unit level. The integration test covers it end-to-end, but a unit assertion catches a missed wire-up in a few ms instead of a 30s network test.Add to the happy-path test:
While we're here: with
--jsonset,pull.tsreturns before callingextractBundle, sompak pull --jsonstill doesn't fix #60 for scripted callers. Worth either moving the extract above the JSON return, or adding a--jsontest that asserts the asymmetry so the choice is recorded.