-
Notifications
You must be signed in to change notification settings - Fork 6
feat: cache vp CLI installation to speed up setup #8
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bd9b9bf
feat: cache vp CLI installation to speed up setup
fengmk2 0ca451e
fix: use os.homedir() fallback in getVitePlusHome()
fengmk2 51d8ec1
fix: cache only the version-specific directory, not entire ~/.vite-plus
fengmk2 2d17a07
fix: add node-version to vp cache key, revert version-dir approach
fengmk2 3fbe800
Fixup
fengmk2 7998294
fix: resolve all dist-tags (not just latest) to precise versions for …
fengmk2 6875014
docs: document Vite+ installation caching in README
fengmk2 8891bd4
Potential fix for pull request finding
fengmk2 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,190 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from "vite-plus/test"; | ||
| import { arch } from "node:os"; | ||
| import { resolveVersion, restoreVpCache, saveVpCache } from "./cache-vp.js"; | ||
| import { State } from "./types.js"; | ||
| import { restoreCache, saveCache } from "@actions/cache"; | ||
| import { saveState, getState, warning } from "@actions/core"; | ||
|
|
||
| // Mock @actions/cache | ||
| vi.mock("@actions/cache", () => ({ | ||
| restoreCache: vi.fn(), | ||
| saveCache: vi.fn(), | ||
| })); | ||
|
|
||
| // Mock @actions/core | ||
| vi.mock("@actions/core", () => ({ | ||
| info: vi.fn(), | ||
| debug: vi.fn(), | ||
| warning: vi.fn(), | ||
| saveState: vi.fn(), | ||
| getState: vi.fn(), | ||
| })); | ||
|
|
||
| describe("resolveVersion", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("should return explicit version as-is", async () => { | ||
| const result = await resolveVersion("0.1.8"); | ||
| expect(result).toBe("0.1.8"); | ||
| }); | ||
|
|
||
| it("should return explicit semver-like versions as-is", async () => { | ||
| const result = await resolveVersion("1.0.0-beta.1"); | ||
| expect(result).toBe("1.0.0-beta.1"); | ||
| }); | ||
|
|
||
| it("should resolve 'latest' from npm registry", async () => { | ||
| const fetchSpy = vi | ||
| .spyOn(globalThis, "fetch") | ||
| .mockResolvedValue(new Response(JSON.stringify({ version: "0.2.0" }), { status: 200 })); | ||
|
|
||
| const result = await resolveVersion("latest"); | ||
| expect(result).toBe("0.2.0"); | ||
| expect(fetchSpy).toHaveBeenCalledWith( | ||
| "https://registry.npmjs.org/vite-plus/latest", | ||
| expect.objectContaining({ signal: expect.any(AbortSignal) }), | ||
| ); | ||
| }); | ||
|
|
||
| it("should return undefined when fetch fails", async () => { | ||
| vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("network error")); | ||
|
|
||
| const result = await resolveVersion("latest"); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return undefined when fetch returns non-ok status", async () => { | ||
| vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("Not Found", { status: 404 })); | ||
|
|
||
| const result = await resolveVersion("latest"); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return undefined for empty string input", async () => { | ||
| vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("should not be called")); | ||
|
|
||
| const result = await resolveVersion(""); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("restoreVpCache", () => { | ||
| beforeEach(() => { | ||
| vi.stubEnv("RUNNER_OS", "Linux"); | ||
| vi.stubEnv("HOME", "/home/runner"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("should return true on cache hit", async () => { | ||
| const expectedKey = `setup-vp-Linux-${arch()}-0.1.8-node20`; | ||
| vi.mocked(restoreCache).mockResolvedValue(expectedKey); | ||
|
|
||
| const result = await restoreVpCache("0.1.8", "20"); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(saveState).toHaveBeenCalledWith(State.VpCachePrimaryKey, expectedKey); | ||
| expect(saveState).toHaveBeenCalledWith(State.VpCacheMatchedKey, expectedKey); | ||
| }); | ||
|
|
||
| it("should include node version in cache key", async () => { | ||
| vi.mocked(restoreCache).mockResolvedValue(undefined); | ||
|
|
||
| await restoreVpCache("0.1.8", "22"); | ||
|
|
||
| expect(restoreCache).toHaveBeenCalledWith( | ||
| ["/home/runner/.vite-plus"], | ||
| `setup-vp-Linux-${arch()}-0.1.8-node22`, | ||
| ); | ||
| }); | ||
|
|
||
| it("should handle empty node version", async () => { | ||
| vi.mocked(restoreCache).mockResolvedValue(undefined); | ||
|
|
||
| await restoreVpCache("0.1.8", ""); | ||
|
|
||
| expect(restoreCache).toHaveBeenCalledWith( | ||
| ["/home/runner/.vite-plus"], | ||
| `setup-vp-Linux-${arch()}-0.1.8-node`, | ||
| ); | ||
| }); | ||
|
|
||
| it("should return false on cache miss", async () => { | ||
| vi.mocked(restoreCache).mockResolvedValue(undefined); | ||
|
|
||
| const result = await restoreVpCache("0.1.8", "20"); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| it("should return false and warn on cache restore error", async () => { | ||
| vi.mocked(restoreCache).mockRejectedValue(new Error("cache error")); | ||
|
|
||
| const result = await restoreVpCache("0.1.8", "20"); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(warning).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("saveVpCache", () => { | ||
| beforeEach(() => { | ||
| vi.stubEnv("HOME", "/home/runner"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("should skip when no primary key", async () => { | ||
| vi.mocked(getState).mockReturnValue(""); | ||
|
|
||
| await saveVpCache(); | ||
|
|
||
| expect(saveCache).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should skip when primary key matches matched key", async () => { | ||
| const key = `setup-vp-Linux-${arch()}-0.1.8-node20`; | ||
| vi.mocked(getState).mockImplementation((k: string) => { | ||
| if (k === State.VpCachePrimaryKey) return key; | ||
| if (k === State.VpCacheMatchedKey) return key; | ||
| return ""; | ||
| }); | ||
|
|
||
| await saveVpCache(); | ||
|
|
||
| expect(saveCache).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should save cache on cache miss", async () => { | ||
| const key = `setup-vp-Linux-${arch()}-0.1.8-node20`; | ||
| vi.mocked(getState).mockImplementation((k: string) => { | ||
| if (k === State.VpCachePrimaryKey) return key; | ||
| return ""; | ||
| }); | ||
| vi.mocked(saveCache).mockResolvedValue(12345); | ||
|
|
||
| await saveVpCache(); | ||
|
|
||
| expect(saveCache).toHaveBeenCalledWith(["/home/runner/.vite-plus"], key); | ||
| }); | ||
|
|
||
| it("should handle save errors gracefully", async () => { | ||
| vi.mocked(getState).mockImplementation((k: string) => { | ||
| if (k === State.VpCachePrimaryKey) return `setup-vp-Linux-${arch()}-0.1.8-node20`; | ||
| return ""; | ||
| }); | ||
| vi.mocked(saveCache).mockRejectedValue(new Error("ReserveCacheError")); | ||
|
|
||
| await saveVpCache(); | ||
|
|
||
| expect(warning).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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,79 @@ | ||
| import { restoreCache, saveCache } from "@actions/cache"; | ||
| import { info, debug, saveState, getState, warning } from "@actions/core"; | ||
| import { arch, platform } from "node:os"; | ||
| import { State } from "./types.js"; | ||
| import { getVitePlusHome } from "./utils.js"; | ||
|
|
||
| /** | ||
| * Resolve "latest" to a specific version number via npm registry. | ||
| * Returns undefined on failure so the caller can fall back to installing without cache. | ||
| */ | ||
| export async function resolveVersion(versionInput: string): Promise<string | undefined> { | ||
| if (versionInput && versionInput !== "latest") { | ||
| return versionInput; | ||
| } | ||
|
|
||
| try { | ||
| const response = await fetch("https://registry.npmjs.org/vite-plus/latest", { | ||
fengmk2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| signal: AbortSignal.timeout(10_000), | ||
| }); | ||
| if (!response.ok) throw new Error(`HTTP ${response.status}`); | ||
| const data = (await response.json()) as { version: string }; | ||
| info(`Resolved latest vp version: ${data.version}`); | ||
| return data.version; | ||
| } catch (error) { | ||
| warning(`Failed to resolve latest vp version: ${error}. Skipping vp cache.`); | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| export async function restoreVpCache(version: string, nodeVersion: string): Promise<boolean> { | ||
| const vpHome = getVitePlusHome(); | ||
| const runnerOS = process.env.RUNNER_OS || platform(); | ||
| const runnerArch = arch(); | ||
| const primaryKey = `setup-vp-${runnerOS}-${runnerArch}-${version}-node${nodeVersion}`; | ||
fengmk2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
fengmk2 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+34
to
+39
|
||
| debug(`Vp cache key: ${primaryKey}`); | ||
| debug(`Vp cache path: ${vpHome}`); | ||
| saveState(State.VpCachePrimaryKey, primaryKey); | ||
|
|
||
| try { | ||
| const matchedKey = await restoreCache([vpHome], primaryKey); | ||
| if (matchedKey) { | ||
| info(`Vite+ restored from cache (key: ${matchedKey})`); | ||
| saveState(State.VpCacheMatchedKey, matchedKey); | ||
| return true; | ||
| } | ||
| } catch (error) { | ||
| warning(`Failed to restore vp cache: ${error}`); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| export async function saveVpCache(): Promise<void> { | ||
| const primaryKey = getState(State.VpCachePrimaryKey); | ||
| const matchedKey = getState(State.VpCacheMatchedKey); | ||
|
|
||
| if (!primaryKey) { | ||
| debug("No vp cache key found. Skipping save."); | ||
| return; | ||
| } | ||
|
|
||
| if (primaryKey === matchedKey) { | ||
| info(`Vp cache hit on primary key "${primaryKey}". Skipping save.`); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const vpHome = getVitePlusHome(); | ||
| const cacheId = await saveCache([vpHome], primaryKey); | ||
| if (cacheId === -1) { | ||
| warning("Vp cache save failed or was skipped."); | ||
| return; | ||
| } | ||
| info(`Vp cache saved with key: ${primaryKey}`); | ||
| } catch (error) { | ||
| warning(`Failed to save vp cache: ${String(error)}`); | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.