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
124 changes: 62 additions & 62 deletions dist/index.mjs

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions src/install-viteplus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, it, expect, afterEach, vi } from "vite-plus/test";
import { exec } from "@actions/exec";
import { warning } from "@actions/core";
import { installVitePlus } from "./install-viteplus.js";
import type { Inputs } from "./types.js";

vi.mock("@actions/core", () => ({
info: vi.fn(),
warning: vi.fn(),
addPath: vi.fn(),
}));

vi.mock("@actions/exec", () => ({
exec: vi.fn(),
}));
Comment on lines +1 to +15

vi.mock("node:timers/promises", () => ({
setTimeout: vi.fn().mockResolvedValue(undefined),
}));

const baseInputs: Inputs = {
version: "latest",
nodeVersion: undefined,
nodeVersionFile: undefined,
workingDirectory: undefined,
runInstall: [],
cache: false,
cacheDependencyPath: undefined,
registryUrl: undefined,
scope: undefined,
};

describe("installVitePlus", () => {
afterEach(() => {
vi.resetAllMocks();
});

it("should succeed on first attempt without retrying", async () => {
vi.mocked(exec).mockResolvedValueOnce(0);

await installVitePlus(baseInputs);

expect(exec).toHaveBeenCalledTimes(1);
expect(warning).not.toHaveBeenCalled();
});

it("should retry on transient failure and eventually succeed", async () => {
vi.mocked(exec).mockResolvedValueOnce(6).mockResolvedValueOnce(6).mockResolvedValueOnce(0);

await installVitePlus(baseInputs);

expect(exec).toHaveBeenCalledTimes(3);
expect(warning).toHaveBeenCalledTimes(2);
});

it("should throw after exhausting all retries", async () => {
vi.mocked(exec).mockResolvedValue(6);

await expect(installVitePlus(baseInputs)).rejects.toThrow(/after 3 attempts/);
expect(exec).toHaveBeenCalledTimes(3);
});

it("should retry when exec itself throws (e.g. process spawn error)", async () => {
vi.mocked(exec).mockRejectedValueOnce(new Error("spawn bash ENOENT")).mockResolvedValueOnce(0);

await installVitePlus(baseInputs);

expect(exec).toHaveBeenCalledTimes(2);
expect(warning).toHaveBeenCalledTimes(1);
});
});
54 changes: 41 additions & 13 deletions src/install-viteplus.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { info, addPath } from "@actions/core";
import { info, warning, addPath } from "@actions/core";
import { exec } from "@actions/exec";
import { join } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import type { Inputs } from "./types.js";
import { DISPLAY_NAME } from "./types.js";
import { getVitePlusHome } from "./utils.js";

const INSTALL_URL_SH = "https://viteplus.dev/install.sh";
const INSTALL_URL_PS1 = "https://viteplus.dev/install.ps1";
const INSTALL_MAX_ATTEMPTS = 3;
const INSTALL_RETRY_DELAY_MS = 2000;

export async function installVitePlus(inputs: Inputs): Promise<void> {
const { version } = inputs;
Expand All @@ -15,24 +18,49 @@ export async function installVitePlus(inputs: Inputs): Promise<void> {

// TODO: Remove VITE_PLUS_VERSION once vite-plus versions before the VP_* env var
// rename (see https://github.com/voidzero-dev/vite-plus/pull/1166) are no longer supported.
const env = { ...process.env, VP_VERSION: version, VITE_PLUS_VERSION: version };
let exitCode: number;
const env = {
...process.env,
VP_VERSION: version,
VITE_PLUS_VERSION: version,
} as { [key: string]: string };

let failureReason = "";
for (let attempt = 1; attempt <= INSTALL_MAX_ATTEMPTS; attempt++) {
try {
const exitCode = await runInstallCommand(env);
if (exitCode === 0) {
ensureVitePlusBinInPath();
return;
}
failureReason = `exit code ${exitCode}`;
} catch (error) {
failureReason = error instanceof Error ? error.message : String(error);
}

if (attempt < INSTALL_MAX_ATTEMPTS) {
const delay = INSTALL_RETRY_DELAY_MS * attempt;
warning(
`Failed to install ${DISPLAY_NAME} (${failureReason}). Retrying in ${delay}ms... (attempt ${attempt + 1}/${INSTALL_MAX_ATTEMPTS})`,
);
await sleep(delay);
}
}

throw new Error(
`Failed to install ${DISPLAY_NAME} after ${INSTALL_MAX_ATTEMPTS} attempts: ${failureReason}`,
);
}

async function runInstallCommand(env: { [key: string]: string }): Promise<number> {
const options = { env, ignoreReturnCode: true };
if (process.platform === "win32") {
exitCode = await exec(
return exec(
"pwsh",
["-Command", `& ([scriptblock]::Create((irm ${INSTALL_URL_PS1})))`],
{ env },
options,
);
} else {
exitCode = await exec("bash", ["-c", `curl -fsSL ${INSTALL_URL_SH} | bash`], { env });
}

if (exitCode !== 0) {
throw new Error(`Failed to install ${DISPLAY_NAME}. Exit code: ${exitCode}`);
}

ensureVitePlusBinInPath();
return exec("bash", ["-c", `curl -fsSL ${INSTALL_URL_SH} | bash`], options);
}

function ensureVitePlusBinInPath(): void {
Expand Down
Loading