Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions packages/cli/src/commands/packages/outdated.test.ts
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");
});
});
70 changes: 70 additions & 0 deletions packages/cli/src/commands/packages/outdated.ts
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> {
Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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.`);
}
2 changes: 1 addition & 1 deletion packages/cli/src/commands/packages/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { homedir } from "os";
import { join } from "path";
import {
parsePackageSpec,
getCacheDir,
resolveArgs,
resolveWorkspace,
substituteUserConfig,
substituteEnvVars,
getLocalCacheDir,
localBundleNeedsExtract,
} from "./run.js";
import { getCacheDir } from "../../utils/cache.js";

describe("parsePackageSpec", () => {
describe("scoped packages", () => {
Expand Down
Loading
Loading