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
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ steps:
node-version-file: ".node-version"
```

### With Working Directory

```yaml
steps:
- uses: actions/checkout@v6
- uses: voidzero-dev/setup-vp@v1
with:
working-directory: web
node-version-file: ".nvmrc"
cache: true
run-install: true
```

### With Caching and Install

```yaml
Expand Down Expand Up @@ -117,16 +130,19 @@ jobs:

## Inputs

| Input | Description | Required | Default |
| ----------------------- | --------------------------------------------------------------------------------------------------------- | -------- | ------------- |
| `version` | Version of Vite+ to install | No | `latest` |
| `node-version` | Node.js version to install via `vp env use` | No | Latest LTS |
| `node-version-file` | Path to file containing Node.js version (`.nvmrc`, `.node-version`, `.tool-versions`, `package.json`) | No | |
| `run-install` | Run `vp install` after setup. Accepts boolean or YAML object with `cwd`/`args` | No | `true` |
| `cache` | Enable caching of project dependencies | No | `false` |
| `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 | |
| Input | Description | Required | Default |
| ----------------------- | ----------------------------------------------------------------------------------------------------------- | -------- | -------------- |
| `version` | Version of Vite+ to install | No | `latest` |
| `node-version` | Node.js version to install via `vp env use` | No | Latest LTS |
| `node-version-file` | Path to file containing Node.js version (`.nvmrc`, `.node-version`, `.tool-versions`, `package.json`) | No | |
| `working-directory` | Project directory used for relative paths, lockfile auto-detection, environment checks, and default install | No | Workspace root |
| `run-install` | Run `vp install` after setup. Accepts boolean or YAML object with `cwd`/`args` | No | `true` |
| `cache` | Enable caching of project dependencies | No | `false` |
| `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 | |

When `working-directory` is set, relative `run-install.cwd`, `node-version-file`, and `cache-dependency-path` values are resolved from that directory.

## Outputs

Expand All @@ -149,6 +165,8 @@ When `cache: true` is set, the action additionally caches project dependencies b

The dependency cache key format is: `vite-plus-{OS}-{arch}-{pm}-{lockfile-hash}`

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.

## Example Workflow
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
node-version-file:
description: "Path to file containing the Node.js version spec (.nvmrc, .node-version, .tool-versions, package.json). Ignored when node-version is specified."
required: false
working-directory:
description: "Project directory to use for relative paths, lockfile auto-detection, environment checks, and default `vp install` execution."
required: false
Comment thread
hyoban marked this conversation as resolved.
cache:
description: "Enable caching of project dependencies"
required: false
Expand Down
146 changes: 73 additions & 73 deletions dist/index.mjs

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/cache-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import { restoreCache as restoreCacheAction } from "@actions/cache";
import { hashFiles } from "@actions/glob";
import { warning, info, debug, saveState, setOutput } from "@actions/core";
import { arch, platform } from "node:os";
import { dirname } from "node:path";
import type { Inputs } from "./types.js";
import { State, Outputs } from "./types.js";
import { detectLockFile, getCacheDirectories, getCacheDirectoryCwd } from "./utils.js";
import { detectLockFile, getCacheDirectories, getConfiguredProjectDir } from "./utils.js";

export async function restoreCache(inputs: Inputs): Promise<void> {
const projectDir = getConfiguredProjectDir(inputs);

// Detect lock file
const lockFile = detectLockFile(inputs.cacheDependencyPath);
const lockFile = detectLockFile(inputs.cacheDependencyPath, projectDir);
if (!lockFile) {
const message = inputs.cacheDependencyPath
? `No lock file found for cache-dependency-path: ${inputs.cacheDependencyPath}. Skipping cache restore.`
: "No lock file found in workspace root. Skipping cache restore.";
: `No lock file found in project directory: ${projectDir}. Skipping cache restore.`;
warning(message);
setOutput(Outputs.CacheHit, false);
return;
}

info(`Using lock file: ${lockFile.path}`);
const cacheCwd = getCacheDirectoryCwd(lockFile.path);
const cacheCwd = dirname(lockFile.path);
info(`Resolving dependency cache directory in: ${cacheCwd}`);

// Get cache directories based on lock file type
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import { State, Outputs } from "./types.js";
import type { Inputs } from "./types.js";
import { resolveNodeVersionFile } from "./node-version-file.js";
import { configAuthentication } from "./auth.js";
import { getConfiguredProjectDir } from "./utils.js";

async function runMain(inputs: Inputs): Promise<void> {
// Mark that post action should run
saveState(State.IsPost, "true");
const projectDir = getConfiguredProjectDir(inputs);

// Step 1: Resolve Node.js version (needed for cache key)
let nodeVersion = inputs.nodeVersion;
if (!nodeVersion && inputs.nodeVersionFile) {
nodeVersion = resolveNodeVersionFile(inputs.nodeVersionFile);
nodeVersion = resolveNodeVersionFile(inputs.nodeVersionFile, projectDir);
}

// Step 2: Install Vite+
Expand Down Expand Up @@ -45,12 +47,12 @@ async function runMain(inputs: Inputs): Promise<void> {
}

// Print version info at the end
await printViteVersion();
await printViteVersion(projectDir);
}

async function printViteVersion(): Promise<void> {
async function printViteVersion(cwd: string): Promise<void> {
try {
const result = await getExecOutput("vp", ["--version"], { silent: true });
const result = await getExecOutput("vp", ["--version"], { cwd, silent: true });
const versionOutput = result.stdout.trim();
info(versionOutput);

Expand Down
13 changes: 13 additions & 0 deletions src/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("getInputs", () => {
version: "latest",
nodeVersion: undefined,
nodeVersionFile: undefined,
workingDirectory: undefined,
runInstall: [],
cache: false,
cacheDependencyPath: undefined,
Expand Down Expand Up @@ -128,4 +129,16 @@ describe("getInputs", () => {

expect(inputs.cacheDependencyPath).toBe("custom-lock.yaml");
});

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

const inputs = getInputs();

expect(inputs.workingDirectory).toBe("web");
});
});
1 change: 1 addition & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function getInputs(): Inputs {
version: getInput("version") || "latest",
nodeVersion: getInput("node-version") || undefined,
nodeVersionFile: getInput("node-version-file") || undefined,
workingDirectory: getInput("working-directory") || undefined,
runInstall: parseRunInstall(getInput("run-install")),
cache: getBooleanInput("cache"),
cacheDependencyPath: getInput("cache-dependency-path") || undefined,
Expand Down
8 changes: 8 additions & 0 deletions src/node-version-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe("resolveNodeVersionFile", () => {
expect(readFileSync).toHaveBeenCalledWith("/custom/path/.nvmrc", "utf-8");
});

it("should resolve relative path against an explicit base directory", () => {
vi.mocked(readFileSync).mockReturnValue("20.0.0\n");

resolveNodeVersionFile(".nvmrc", "/workspace/web");

expect(readFileSync).toHaveBeenCalledWith("/workspace/web/.nvmrc", "utf-8");
});

it("should throw if file does not exist", () => {
vi.mocked(readFileSync).mockImplementation(() => {
throw new Error("ENOENT");
Expand Down
6 changes: 3 additions & 3 deletions src/node-version-file.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { info } from "@actions/core";
import { readFileSync } from "node:fs";
import { basename } from "node:path";
import { resolveWorkspacePath } from "./utils.js";
import { getWorkspaceDir, resolvePath } from "./utils.js";

/**
* Resolve a Node.js version from a version file.
*
* Supports: .nvmrc, .node-version, .tool-versions, package.json
*/
export function resolveNodeVersionFile(filePath: string): string {
const fullPath = resolveWorkspacePath(filePath);
export function resolveNodeVersionFile(filePath: string, baseDir?: string): string {
const fullPath = resolvePath(filePath, baseDir || getWorkspaceDir());

let content: string;
try {
Expand Down
5 changes: 4 additions & 1 deletion src/run-install.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { startGroup, endGroup, setFailed, info } from "@actions/core";
import { exec } from "@actions/exec";
import type { Inputs } from "./types.js";
import { getConfiguredProjectDir, getInstallCwd } from "./utils.js";

export async function runViteInstall(inputs: Inputs): Promise<void> {
const projectDir = getConfiguredProjectDir(inputs);

for (const options of inputs.runInstall) {
const args = ["install"];
if (options.args) {
args.push(...options.args);
}

const cwd = options.cwd || process.env.GITHUB_WORKSPACE || process.cwd();
const cwd = getInstallCwd(projectDir, options.cwd);
const cmdStr = `vp ${args.join(" ")}`;

startGroup(`Running ${cmdStr} in ${cwd}...`);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Inputs {
readonly version: string;
readonly nodeVersion?: string;
readonly nodeVersionFile?: string;
readonly workingDirectory?: string;
readonly runInstall: RunInstall[];
readonly cache: boolean;
readonly cacheDependencyPath?: string;
Expand Down
Loading
Loading