Skip to content
Merged
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
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ jobs:
echo "OK: ${{ matrix.scenario }} resolved to $ACTUAL"

test-cache-pnpm:
strategy:
fail-fast: false
matrix:
cache-save: [true, false]
runs-on: ubuntu-latest
steps:
- uses: taiki-e/checkout-action@7d1e50e93dc4fb3bba58f85018fadf77898aee8b # v1.4.2
Expand All @@ -348,12 +352,13 @@ jobs:
echo '{"name":"test-project","private":true}' > package.json
touch pnpm-lock.yaml

- name: Setup Vite+ with pnpm cache
- name: Setup Vite+ with pnpm cache (cache-save=${{ matrix.cache-save }})
uses: ./
id: setup
with:
run-install: false
cache: true
cache-save: ${{ matrix.cache-save }}
cache-dependency-path: test-project/pnpm-lock.yaml

- name: Verify installation
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ jobs:
| `run-install` | Run `vp install` after setup. Accepts boolean or YAML object with `cwd`/`args` | No | `true` |
| `sfw` | Wrap `vp install` with [Socket Firewall Free](https://docs.socket.dev/docs/socket-firewall-free) (`sfw`) | No | `false` |
| `cache` | Enable caching of project dependencies | No | `false` |
| `cache-save` | Save the dependency cache in the post action. Has no effect when `cache` is `false` | No | `true` |
| `cache-dependency-path` | Path to lock file for cache key generation | No | Auto-detected |
| `registry-url` | Optional registry to set up for auth. Sets the registry in `.npmrc` and reads auth from `NODE_AUTH_TOKEN` | No | |
| `scope` | Optional scope for scoped registries. Falls back to repo owner for GitHub Packages | No | |
Expand Down Expand Up @@ -378,6 +379,27 @@ When `working-directory` is set, lockfile auto-detection runs in that directory.

When `cache-dependency-path` points to a lock file in a subdirectory, the action resolves the package-manager cache directory from that lock file's directory.

### Control cache saving

Set `cache-save: false` to restore an existing dependency cache without writing a new cache. The `cache` input remains the main switch for both operations:

| `cache` | `cache-save` | Restore | Save |
| ------- | ----------------- | ------- | ---- |
| `false` | Any value | No | No |
| `true` | Omitted or `true` | Yes | Yes |
| `true` | `false` | Yes | No |

For example, this workflow restores caches on every run but saves them only from the `main` branch:

```yaml
- uses: voidzero-dev/setup-vp@v1.18.0
with:
cache: true
cache-save: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
```

Disabling cache saving doesn't change the `cache-hit` output, which continues to report whether the action restored a matching cache.

## GitLab CI/CD

setup-vp also provides a GitLab CI/CD remote template hosted from this GitHub repository. Because this repository is not a GitLab CI/CD component project, GitLab users should load it with `include:remote` instead of `include:component`.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ inputs:
description: "Enable caching of project dependencies"
required: false
default: "false"
cache-save:
description: "Whether to save the dependency cache in the post action. Has no effect when cache is false."
required: false
default: "true"
cache-dependency-path:
description: "Path to lock file for cache key generation. Auto-detected if not specified."
required: false
Expand Down
4 changes: 2 additions & 2 deletions dist/index.mjs

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";

vi.mock("@actions/core", async (importOriginal) => {
const actual = await importOriginal<typeof import("@actions/core")>();
return {
...actual,
getState: vi.fn(() => "true"),
info: vi.fn(),
};
});
vi.mock("./inputs.js", () => ({
getInputs: () => ({
version: "",
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
}),
}));
vi.mock("./cache-save.js", () => ({
saveCache: vi.fn(),
}));

import { info } from "@actions/core";
import { saveCache } from "./cache-save.js";
import { runPost } from "./index.js";
import type { Inputs } from "./types.js";

const mockedInfo = vi.mocked(info);
const mockedSaveCache = vi.mocked(saveCache);

const inputs = (cache: boolean, cacheSave: boolean): Inputs => ({
version: "",
runInstall: [],
sfw: false,
cache,
cacheSave,
});

describe("runPost", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("skips silently when caching is disabled", async () => {
await runPost(inputs(false, true));

expect(mockedSaveCache).not.toHaveBeenCalled();
expect(mockedInfo).not.toHaveBeenCalled();
});

it("logs and skips when cache saving is disabled", async () => {
await runPost(inputs(true, false));

expect(mockedSaveCache).not.toHaveBeenCalled();
expect(mockedInfo).toHaveBeenCalledWith("Cache saving is disabled. Skipping cache save.");
});

it("saves the cache when caching and cache saving are enabled", async () => {
await runPost(inputs(true, true));

expect(mockedSaveCache).toHaveBeenCalledOnce();
expect(mockedInfo).not.toHaveBeenCalled();
});
});
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ async function printViteVersion(cwd: string): Promise<void> {
}
}

async function runPost(inputs: Inputs): Promise<void> {
if (inputs.cache) {
await saveCache();
export async function runPost(inputs: Inputs): Promise<void> {
if (!inputs.cache) return;

if (!inputs.cacheSave) {
info("Cache saving is disabled. Skipping cache save.");
return;
}

await saveCache();
}

async function main(): Promise<void> {
Expand Down
13 changes: 13 additions & 0 deletions src/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("getInputs", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: false,
cacheDependencyPath: undefined,
});
});
Expand Down Expand Up @@ -120,6 +121,18 @@ describe("getInputs", () => {
expect(inputs.cache).toBe(true);
});

it("should parse cache-save input", () => {
vi.mocked(getInput).mockReturnValue("");
vi.mocked(getBooleanInput).mockImplementation((name) => {
if (name === "cache-save") return true;
return false;
});

const inputs = getInputs();

expect(inputs.cacheSave).toBe(true);
});

it("should parse sfw input", () => {
vi.mocked(getInput).mockReturnValue("");
vi.mocked(getBooleanInput).mockImplementation((name) => {
Expand Down
1 change: 1 addition & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function getInputs(): Inputs {
runInstall: parseRunInstall(getInput("run-install")),
sfw: getBooleanInput("sfw"),
cache: getBooleanInput("cache"),
cacheSave: getBooleanInput("cache-save"),
cacheDependencyPath: getInput("cache-dependency-path") || undefined,
registryUrl: getInput("registry-url") || undefined,
scope: getInput("scope") || undefined,
Expand Down
1 change: 1 addition & 0 deletions src/install-sfw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function makeInputs(overrides: Partial<Inputs> = {}): Inputs {
runInstall: [{}],
sfw: true,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/install-viteplus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const baseInputs: Inputs = {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/run-install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const baseInputs: Inputs = {
runInstall: [{}],
sfw: true,
cache: false,
cacheSave: true,
};

const mockedExec = vi.mocked(getExecOutput);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Inputs {
readonly runInstall: RunInstall[];
readonly sfw: boolean;
readonly cache: boolean;
readonly cacheSave: boolean;
readonly cacheDependencyPath?: string;
readonly registryUrl?: string;
readonly scope?: string;
Expand Down
5 changes: 5 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ describe("getConfiguredProjectDir", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand All @@ -315,6 +316,7 @@ describe("getConfiguredProjectDir", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand All @@ -334,6 +336,7 @@ describe("getConfiguredProjectDir", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand All @@ -355,6 +358,7 @@ describe("getConfiguredProjectDir", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand Down Expand Up @@ -389,6 +393,7 @@ describe("resolvePath", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/version-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ describe("resolveVitePlusVersion (precedence)", () => {
runInstall: [],
sfw: false,
cache: false,
cacheSave: true,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
Expand Down
Loading