-
Notifications
You must be signed in to change notification settings - Fork 0
Add bundle update awareness: outdated, update commands and async check #48
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
5
commits into
main
Choose a base branch
from
issue-37-cli-add-bundle-update-awareness
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
5 commits
Select commit
Hold shift + click to select a range
0310f2a
Extract shared cache helpers into utils/cache.ts
shwetank-dev 35d38ea
Add async update check during mpak run for registry bundles
shwetank-dev d70ddcd
Add mpak outdated and mpak update commands (#37)
shwetank-dev 97a7cfe
Add tests for listCachedBundles, getOutdatedBundles, and handleUpdate
shwetank-dev 9d99b9d
Consolidate download/extract logic into shared cache helpers
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,122 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { getOutdatedBundles } from "./outdated.js"; | ||
|
|
||
| vi.mock("../../utils/cache.js", () => ({ | ||
| listCachedBundles: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../../utils/client.js", () => ({ | ||
| createClient: vi.fn(), | ||
| })); | ||
|
|
||
| import { listCachedBundles } from "../../utils/cache.js"; | ||
| import { createClient } from "../../utils/client.js"; | ||
|
|
||
| const mockListCachedBundles = vi.mocked(listCachedBundles); | ||
| const mockCreateClient = vi.mocked(createClient); | ||
|
|
||
| function makeMockClient(registry: Record<string, string>) { | ||
| return { | ||
| getBundle: vi.fn(async (name: string) => { | ||
| const version = registry[name]; | ||
| if (!version) throw new Error(`Not found: ${name}`); | ||
| return { latest_version: version }; | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("getOutdatedBundles", () => { | ||
| it("returns empty array when no bundles are cached", async () => { | ||
| mockListCachedBundles.mockReturnValue([]); | ||
|
|
||
| const result = await getOutdatedBundles(); | ||
| expect(result).toEqual([]); | ||
| expect(mockCreateClient).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns empty array when all bundles are up to date", async () => { | ||
| mockListCachedBundles.mockReturnValue([ | ||
| { name: "@scope/a", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/a" }, | ||
| { name: "@scope/b", version: "2.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/b" }, | ||
| ]); | ||
| mockCreateClient.mockReturnValue(makeMockClient({ | ||
| "@scope/a": "1.0.0", | ||
| "@scope/b": "2.0.0", | ||
| }) as never); | ||
|
|
||
| const result = await getOutdatedBundles(); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns outdated bundles with current and latest versions", async () => { | ||
| mockListCachedBundles.mockReturnValue([ | ||
| { name: "@scope/a", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/a" }, | ||
| { name: "@scope/b", version: "2.0.0", pulledAt: "2025-02-01T00:00:00.000Z", cacheDir: "/cache/b" }, | ||
| ]); | ||
| mockCreateClient.mockReturnValue(makeMockClient({ | ||
| "@scope/a": "1.1.0", | ||
| "@scope/b": "2.0.0", | ||
| }) as never); | ||
|
|
||
| const result = await getOutdatedBundles(); | ||
| expect(result).toEqual([ | ||
| { | ||
| name: "@scope/a", | ||
| current: "1.0.0", | ||
| latest: "1.1.0", | ||
| pulledAt: "2025-01-01T00:00:00.000Z", | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("returns multiple outdated bundles sorted by name", async () => { | ||
| mockListCachedBundles.mockReturnValue([ | ||
| { name: "@scope/zebra", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/z" }, | ||
| { name: "@scope/alpha", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/a" }, | ||
| ]); | ||
| mockCreateClient.mockReturnValue(makeMockClient({ | ||
| "@scope/zebra": "2.0.0", | ||
| "@scope/alpha": "1.1.0", | ||
| }) as never); | ||
|
|
||
| const result = await getOutdatedBundles(); | ||
| expect(result).toHaveLength(2); | ||
| expect(result[0]!.name).toBe("@scope/alpha"); | ||
| expect(result[1]!.name).toBe("@scope/zebra"); | ||
| }); | ||
|
|
||
| it("skips bundles that fail to resolve from registry", async () => { | ||
| mockListCachedBundles.mockReturnValue([ | ||
| { name: "@scope/exists", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/e" }, | ||
| { name: "@scope/deleted", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/d" }, | ||
| ]); | ||
| mockCreateClient.mockReturnValue(makeMockClient({ | ||
| "@scope/exists": "2.0.0", | ||
| // @scope/deleted not in registry — getBundle will throw | ||
| }) as never); | ||
|
|
||
| const result = await getOutdatedBundles(); | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0]!.name).toBe("@scope/exists"); | ||
| }); | ||
|
|
||
| it("checks all bundles in parallel", async () => { | ||
| const getBundle = vi.fn(async (name: string) => { | ||
| return { latest_version: name === "@scope/a" ? "2.0.0" : "1.0.0" }; | ||
| }); | ||
| mockListCachedBundles.mockReturnValue([ | ||
| { name: "@scope/a", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/a" }, | ||
| { name: "@scope/b", version: "1.0.0", pulledAt: "2025-01-01T00:00:00.000Z", cacheDir: "/cache/b" }, | ||
| ]); | ||
| mockCreateClient.mockReturnValue({ getBundle } as never); | ||
|
|
||
| await getOutdatedBundles(); | ||
| expect(getBundle).toHaveBeenCalledTimes(2); | ||
| expect(getBundle).toHaveBeenCalledWith("@scope/a"); | ||
| expect(getBundle).toHaveBeenCalledWith("@scope/b"); | ||
| }); | ||
| }); |
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,70 @@ | ||
| import { listCachedBundles } from "../../utils/cache.js"; | ||
| import { createClient } from "../../utils/client.js"; | ||
| import { table } from "../../utils/format.js"; | ||
|
|
||
| export interface OutdatedEntry { | ||
| name: string; | ||
| current: string; | ||
| latest: string; | ||
| pulledAt: string; | ||
| } | ||
|
|
||
| export interface OutdatedOptions { | ||
| json?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Check all cached registry bundles against the registry and return those | ||
| * that have a newer version available. | ||
| */ | ||
| export async function getOutdatedBundles(): Promise<OutdatedEntry[]> { | ||
| const cached = listCachedBundles(); | ||
| if (cached.length === 0) return []; | ||
|
|
||
| const client = createClient(); | ||
| const results: OutdatedEntry[] = []; | ||
|
|
||
| await Promise.all( | ||
| cached.map(async (bundle) => { | ||
| try { | ||
| const detail = await client.getBundle(bundle.name); | ||
| if (detail.latest_version !== bundle.version) { | ||
| results.push({ | ||
| name: bundle.name, | ||
| current: bundle.version, | ||
| latest: detail.latest_version, | ||
| pulledAt: bundle.pulledAt, | ||
| }); | ||
| } | ||
| } catch { | ||
| // Skip bundles that fail to resolve (e.g. deleted from registry) | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| return results.sort((a, b) => a.name.localeCompare(b.name)); | ||
| } | ||
|
|
||
| export async function handleOutdated(options: OutdatedOptions = {}): Promise<void> { | ||
| process.stderr.write("=> Checking for updates...\n"); | ||
|
|
||
| const outdated = await getOutdatedBundles(); | ||
|
|
||
| if (options.json) { | ||
| console.log(JSON.stringify(outdated, null, 2)); | ||
| return; | ||
| } | ||
|
|
||
| if (outdated.length === 0) { | ||
| console.log("All cached bundles are up to date."); | ||
| return; | ||
| } | ||
|
|
||
| console.log( | ||
| table( | ||
| ["Bundle", "Current", "Latest", "Pulled"], | ||
| outdated.map((e) => [e.name, e.current, e.latest, e.pulledAt]), | ||
| ), | ||
| ); | ||
| console.log(`\n${outdated.length} bundle(s) can be updated. Run 'mpak update' to update all.`); | ||
| } | ||
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.
handleOutdated calls listCachedBundles twice — outdated.ts:51 calls listCachedBundles() to check if empty, then getOutdatedBundles() at line 54 calls it again internally. could pass the list in, or just let getOutdatedBundles return empty.
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.
Done