Skip to content
Closed
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
2 changes: 1 addition & 1 deletion dist/index.mjs

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions src/run-install.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, it, expect, afterEach, vi } from "vite-plus/test";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, join } from "node:path";
import { runViteInstall } from "./run-install.js";
import type { Inputs } from "./types.js";

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

describe("runViteInstall", () => {
let tempDir: string | undefined;

afterEach(() => {
vi.unstubAllEnvs();
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
tempDir = undefined;
}
});

it("stops after the first failed install", async () => {
tempDir = mkdtempSync(join(tmpdir(), "setup-vp-"));
const binDir = join(tempDir, "bin");
const appDir = join(tempDir, "packages", "app");
const libDir = join(tempDir, "packages", "lib");
const callsLog = join(tempDir, "calls.log");

mkdirSync(binDir);
mkdirSync(appDir, { recursive: true });
mkdirSync(libDir, { recursive: true });
writeFileSync(join(appDir, ".fail-vp-install"), "");

writeFileSync(
join(binDir, "vp"),
[
"#!/bin/sh",
'printf "%s %s\\n" "$PWD" "$*" >> "$VP_CALLS_LOG"',
'if [ -f ".fail-vp-install" ]; then',
' echo "install failed" >&2',
" exit 1",
"fi",
"exit 0",
"",
].join("\n"),
);
chmodSync(join(binDir, "vp"), 0o755);

writeFileSync(
join(binDir, "vp.cmd"),
[
"@echo off",
'echo %CD% %*>>"%VP_CALLS_LOG%"',
'if exist ".fail-vp-install" (',
" echo install failed 1>&2",
" exit /b 1",
")",
"exit /b 0",
"",
].join("\r\n"),
);

vi.stubEnv("GITHUB_WORKSPACE", tempDir);
vi.stubEnv("PATH", `${binDir}${delimiter}${process.env.PATH ?? ""}`);
vi.stubEnv("VP_CALLS_LOG", callsLog);

await expect(
runViteInstall({
...baseInputs,
runInstall: [{ cwd: "packages/app" }, { cwd: "packages/lib" }],
}),
).rejects.toThrow(`Command "vp install" (cwd: ${appDir}) exited with code 1`);

const calls = readFileSync(callsLog, "utf8").trim().split(/\r?\n/);
expect(calls).toHaveLength(1);
expect(calls[0]).toContain(`${join("packages", "app")} install`);
});
});
36 changes: 19 additions & 17 deletions src/run-install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { startGroup, endGroup, setFailed, info, error as logError } from "@actions/core";
import { startGroup, endGroup, info, error as logError } from "@actions/core";
import { getExecOutput } from "@actions/exec";
import type { Inputs } from "./types.js";
import { getConfiguredProjectDir, getInstallCwd } from "./utils.js";
Expand All @@ -25,28 +25,30 @@ export async function runViteInstall(inputs: Inputs): Promise<void> {

startGroup(`Running ${cmdStr} in ${cwd}...`);

let result: Awaited<ReturnType<typeof getExecOutput>>;
try {
const { exitCode, stdout, stderr } = await getExecOutput("vp", args, {
result = await getExecOutput("vp", args, {
cwd,
ignoreReturnCode: true,
});
endGroup();

if (exitCode === 0) {
info(`Successfully ran ${cmdStr}`);
continue;
}

const detail = stderr.trim() || stdout.trim();
if (detail) {
logError(tailOutput(detail, MAX_ERROR_TAIL), {
title: `${cmdStr} failed`,
});
}
setFailed(`Command "${cmdStr}" (cwd: ${cwd}) exited with code ${exitCode}`);
} catch (error) {
endGroup();
setFailed(`Failed to run ${cmdStr}: ${String(error)}`);
throw new Error(`Failed to run ${cmdStr}: ${String(error)}`);
}

endGroup();

if (result.exitCode === 0) {
info(`Successfully ran ${cmdStr}`);
continue;
}

const detail = result.stderr.trim() || result.stdout.trim();
if (detail) {
logError(tailOutput(detail, MAX_ERROR_TAIL), {
title: `${cmdStr} failed`,
});
}
throw new Error(`Command "${cmdStr}" (cwd: ${cwd}) exited with code ${result.exitCode}`);
}
}