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
12 changes: 6 additions & 6 deletions dist/azure/index.mjs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/gitlab/index.mjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.mjs

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/ci/cache-snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import { restoreCacheSnapshot } from "./cache-snapshot.js";
import { isWindows } from "./platform.js";

vi.mock("node:fs", async (importOriginal) => {
const fs = await importOriginal<typeof import("node:fs")>();
Expand Down Expand Up @@ -98,7 +99,7 @@ describe("GitLab cache snapshots", () => {
symlinkSync(
targetKind === "absolute" ? packageDir : path.relative(index, packageDir),
link,
process.platform === "win32" ? "junction" : "dir",
isWindows() ? "junction" : "dir",
);

restoreCacheSnapshot(metadata, cache, warn).save();
Expand Down Expand Up @@ -139,7 +140,7 @@ describe("GitLab cache snapshots", () => {
symlinkSync(
targetKind === "absolute" ? packageDir : "../package",
path.join(index, "link"),
process.platform === "win32" ? "junction" : "dir",
isWindows() ? "junction" : "dir",
);
const warn = vi.fn();
restoreCacheSnapshot(metadata, cache, warn).save();
Expand Down Expand Up @@ -179,7 +180,7 @@ describe("GitLab cache snapshots", () => {
mkdirSync(target);
writeFileSync(path.join(target, "value"), "project data");
writeFileSync(path.join(store, "package"), "cached package");
symlinkSync(target, link, process.platform === "win32" ? "junction" : "dir");
symlinkSync(target, link, isWindows() ? "junction" : "dir");
const warn = vi.fn();
restoreCacheSnapshot(metadata, cache, warn).save();
rmSync(store, { recursive: true });
Expand All @@ -200,7 +201,7 @@ describe("GitLab cache snapshots", () => {
expect(warn).not.toHaveBeenCalled();
});

it.skipIf(process.platform !== "win32")(
it.skipIf(!isWindows())(
"repairs a directory link extracted as a file link before its target exists",
() => {
const { root, store, cache, metadata } = fixture();
Expand Down Expand Up @@ -247,7 +248,7 @@ describe("GitLab cache snapshots", () => {
restoreCacheSnapshot(metadata, cache, warn).save();

rmSync(entry, { recursive: true });
symlinkSync(target, entry, process.platform === "win32" ? "junction" : "dir");
symlinkSync(target, entry, isWindows() ? "junction" : "dir");
writeFileSync(lockFile, "second lock");
restoreCacheSnapshot(metadata, cache, warn, false).save();

Expand Down Expand Up @@ -276,7 +277,7 @@ describe("GitLab cache snapshots", () => {
if (entryKind === "directory") {
mkdirSync(target);
writeFileSync(path.join(target, "value"), "external value");
symlinkSync(target, entry, process.platform === "win32" ? "junction" : "dir");
symlinkSync(target, entry, isWindows() ? "junction" : "dir");
} else {
writeFileSync(target, "external value");
symlinkSync(target, entry, "file");
Expand Down Expand Up @@ -313,7 +314,7 @@ describe("GitLab cache snapshots", () => {
const newTarget = path.join(root, "new-target");
const cachedLink = path.join(cache, process.platform, process.arch, "npm", "packages", "link");
const warn = vi.fn();
const linkType = process.platform === "win32" ? "junction" : "dir";
const linkType = isWindows() ? "junction" : "dir";
mkdirSync(oldTarget);
mkdirSync(newTarget);
symlinkSync(oldTarget, link, linkType);
Expand Down
4 changes: 3 additions & 1 deletion src/ci/install-script-urls.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isWindows } from "./platform.js";

// The install script changes with the CLI: a script from main can install an
// older CLI incorrectly. Example: the XDG directory-layout switch in
// voidzero-dev/vite-plus#2346 changes where fresh installs land. When the
Expand Down Expand Up @@ -52,7 +54,7 @@ export function getInstallScriptUrls(
version: string,
platform: NodeJS.Platform = process.platform,
): InstallScriptUrls {
const script = platform === "win32" ? "install.ps1" : "install.sh";
const script = isWindows(platform) ? "install.ps1" : "install.sh";
const ref = installScriptRef(version);
return {
pinned: ref
Expand Down
7 changes: 4 additions & 3 deletions src/ci/install-sfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { spawnSync } from "node:child_process";
import type { get as httpGet } from "node:http";
import { tmpdir } from "node:os";
import path from "node:path";
import { isWindows } from "./platform.js";
import { commandPath } from "./process.js";
import { resolveSfwEnabled } from "./sfw.js";
import type { ExportVariable, InstallCommand, LogFn, RunInstallEntry } from "./types.js";
Expand Down Expand Up @@ -42,7 +43,7 @@ export function getSfwAssetName(platform: NodeJS.Platform, arch: string, isMusl:
if (arch === "x64") {
return isMusl ? "sfw-free-musl-linux-x86_64" : "sfw-free-linux-x86_64";
}
} else if (platform === "win32") {
} else if (isWindows(platform)) {
if (arch === "arm64") return "sfw-free-windows-arm64.exe";
if (arch === "x64") return "sfw-free-windows-x86_64.exe";
}
Expand Down Expand Up @@ -205,10 +206,10 @@ export async function setupSfw(
? path.join(cacheDirectory, SFW_VERSION, asset)
: await mkdtemp(path.join(tmpdir(), "setup-vp-sfw-"));
await mkdir(sfwDir, { recursive: true });
const sfwBin = path.join(sfwDir, platform === "win32" ? "sfw.exe" : "sfw");
const sfwBin = path.join(sfwDir, isWindows(platform) ? "sfw.exe" : "sfw");
const sfwUrl = `${SFW_RELEASE_BASE}/${asset}`;
function activate(): InstallCommand {
const pathSeparator = platform === "win32" ? ";" : ":";
const pathSeparator = isWindows(platform) ? ";" : ":";
env.PATH = `${sfwDir}${pathSeparator}${env.PATH || ""}`;
options.exportVariable?.("PATH", env.PATH);
return "sfw";
Expand Down
7 changes: 4 additions & 3 deletions src/ci/install-viteplus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import { spawnSync } from "node:child_process";
import { getInstallScriptUrls, pkgPrNewCommitSha } from "./install-script-urls.js";
import { isWindows } from "./platform.js";
import {
createVitePlusDirsFile,
getInstallScriptCommand,
Expand All @@ -19,7 +20,7 @@ export function getVitePlusHome(
platform: NodeJS.Platform = process.platform,
env: NodeJS.ProcessEnv = process.env,
): string {
const home = platform === "win32" ? env.USERPROFILE || homedir() : env.HOME || homedir();
const home = isWindows(platform) ? env.USERPROFILE || homedir() : env.HOME || homedir();
return join(home, ".vite-plus");
}

Expand All @@ -37,7 +38,7 @@ function runInstallCommand(
// GitLab PowerShell Desktop runners need not have PowerShell Core installed.
// Only fall back when the executable is missing, not when an installer fails.
if (
platform === "win32" &&
isWindows(platform) &&
(result.error as NodeJS.ErrnoException | undefined)?.code === "ENOENT"
) {
result = spawnSync("powershell.exe", args, spawnOptions);
Expand Down Expand Up @@ -127,7 +128,7 @@ export async function installVitePlus(
dirsFile,
join(getVitePlusHome(platform, targetEnv), "bin"),
);
const separator = platform === "win32" ? ";" : ":";
const separator = isWindows(platform) ? ";" : ":";
if (!targetEnv.PATH?.split(separator).includes(binDir)) {
targetEnv.PATH = `${binDir}${separator}${targetEnv.PATH || ""}`;
prependPath?.(binDir);
Expand Down
15 changes: 15 additions & 0 deletions src/ci/platform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from "vite-plus/test";
import { isWindows } from "./platform.js";

describe("isWindows", () => {
it("returns true for Windows", () => {
expect(isWindows("win32")).toBe(true);
});

it.each(["aix", "darwin", "freebsd", "linux", "openbsd", "sunos"] as const)(
"returns false for %s",
(platform) => {
expect(isWindows(platform)).toBe(false);
},
);
});
3 changes: 3 additions & 0 deletions src/ci/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isWindows(platform: NodeJS.Platform = process.platform): boolean {
return platform === "win32";
}
3 changes: 2 additions & 1 deletion src/ci/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import { isWindows } from "./platform.js";
import { commandPath, getCommandOutput, run, runWithOutput } from "./process.js";

const directories: string[] = [];
Expand All @@ -31,7 +32,7 @@ describe("portable process helpers", () => {
});
});

it.skipIf(process.platform !== "win32")(
it.skipIf(!isWindows())(
"runs vp.exe from a directory with spaces through every helper",
async () => {
const root = mkdtempSync(path.join(tmpdir(), "setup-vp-exe-"));
Expand Down
3 changes: 2 additions & 1 deletion src/ci/process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawn, spawnSync } from "node:child_process";
import type { SpawnOptions, SpawnSyncOptions } from "node:child_process";
import { isWindows } from "./platform.js";

export function run(command: string, args: string[], options: SpawnSyncOptions = {}): void {
const result = spawnSync(command, args, { stdio: "inherit", ...options });
Expand Down Expand Up @@ -32,7 +33,7 @@ export function runWithOutput(
}

export function commandPath(command: string): string | undefined {
if (process.platform === "win32") {
if (isWindows()) {
const result = spawnSync("where", [command], { encoding: "utf8" });
if (result.status === 0) {
const line = result.stdout.trim().split(/\r?\n/)[0]?.trim();
Expand Down
7 changes: 4 additions & 3 deletions src/ci/run-install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "node:path";
import { parse as parseYaml } from "yaml";
import { isWindows } from "./platform.js";
import { runWithOutput } from "./process.js";
import type { InstallCommand, RunInstallEntry, RunInstallInput } from "./types.js";

Expand Down Expand Up @@ -89,13 +90,13 @@ export async function runInstall(
installCommand === "sfw" &&
isSfwVpNotFoundFlake(result.stdout, result.stderr)
) {
const isWindows = (options.platform ?? process.platform) === "win32";
const windows = isWindows(options.platform);
console.warn(
isWindows
windows
? "setup-vp: sfw could not resolve vp; warming the PowerShell command cache and retrying once."
: "setup-vp: sfw could not resolve vp; retrying once.",
);
if (isWindows) {
if (windows) {
try {
await execute("powershell.exe", ["-NoProfile", "-Command", "Get-Command vp"], {
cwd,
Expand Down
5 changes: 3 additions & 2 deletions src/ci/vp-dirs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { isWindows } from "./platform.js";
import { getInstallScriptCommand, parseVitePlusDirs, supportsVitePlusDirs } from "./vp-dirs.js";

describe("Vite+ directory resolution", () => {
Expand Down Expand Up @@ -66,7 +67,7 @@ describe("Vite+ directory resolution", () => {
expect(command.args[1]).toContain('>> "$SETUP_VP_DIRS_FILE"');
});

it.skipIf(process.platform === "win32").each([true, false])(
it.skipIf(isWindows()).each([true, false])(
"isolates inherited nounset and preserves installer failures (detectDirs: %s)",
(detectDirs) => {
const fixture = mkdtempSync(join(tmpdir(), "setup-vp-shell-options-"));
Expand Down Expand Up @@ -165,7 +166,7 @@ printf 'installer completed\\n'
expect(command.args[1]).toContain("Add-Content -LiteralPath $dirsFile -Encoding UTF8");
});

it.skipIf(process.platform === "win32").each([true, false])(
it.skipIf(isWindows()).each([true, false])(
"installs with wget and no curl, preserving download failures (detectDirs: %s)",
(detectDirs) => {
const root = mkdtempSync(join(tmpdir(), "setup-vp-wget-"));
Expand Down
3 changes: 2 additions & 1 deletion src/ci/vp-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { pkgPrNewCommitSha } from "./install-script-urls.js";
import { isWindows } from "./platform.js";
import { parseInstalledVpVersion } from "./version.js";

// Keep installer network calls bounded so a hung source fails over quickly.
Expand Down Expand Up @@ -108,7 +109,7 @@ export function getInstallScriptCommand(
platform: NodeJS.Platform = process.platform,
detectDirs = true,
): { command: string; args: string[] } {
if (platform === "win32") {
if (isWindows(platform)) {
if (!detectDirs) {
return {
command: "pwsh",
Expand Down
13 changes: 7 additions & 6 deletions src/install-sfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { execFileSync } from "node:child_process";
import { chmodSync, existsSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import { isWindows } from "./ci/platform.js";
import { resolveSfwEnabled } from "./ci/sfw.js";
import type { Inputs } from "./types.js";

Expand Down Expand Up @@ -71,7 +72,7 @@ export function getSfwAssetName(platform: NodeJS.Platform, arch: string, isMusl:
if (arch === "x64") {
return isMusl ? "sfw-free-musl-linux-x86_64" : "sfw-free-linux-x86_64";
}
} else if (platform === "win32") {
} else if (isWindows(platform)) {
if (arch === "arm64") return "sfw-free-windows-arm64.exe";
if (arch === "x64") return "sfw-free-windows-x86_64.exe";
}
Expand All @@ -89,7 +90,7 @@ export async function installSfw(): Promise<void> {
const url = `${SFW_RELEASE_BASE}/${assetName}`;
const binDir = getSfwBinDir();
mkdirSync(binDir, { recursive: true });
const binPath = join(binDir, process.platform === "win32" ? "sfw.exe" : "sfw");
const binPath = join(binDir, isWindows() ? "sfw.exe" : "sfw");

// Try the GHA cache first so we don't redownload ~130 MB on every run and
// we get a fallback when the GitHub releases CDN flakes. Key includes the
Expand All @@ -100,7 +101,7 @@ export async function installSfw(): Promise<void> {
try {
const matchedKey = await restoreCache([binDir], cacheKey);
if (matchedKey && existsSync(binPath)) {
if (process.platform !== "win32") {
if (!isWindows()) {
chmodSync(binPath, 0o755);
}
addPath(binDir);
Expand All @@ -121,7 +122,7 @@ export async function installSfw(): Promise<void> {
try {
const exitCode = await runDownloadCommand(url, binPath);
if (exitCode === 0 && existsSync(binPath)) {
if (process.platform !== "win32") {
if (!isWindows()) {
chmodSync(binPath, 0o755);
}
addPath(binDir);
Expand Down Expand Up @@ -161,7 +162,7 @@ export async function installSfw(): Promise<void> {
// Used to detect when the user composed `socketdev/action@<sha>` (or
// installed sfw via some other means) before invoking this action.
export function findSfwOnPath(): string | null {
const lookupCmd = process.platform === "win32" ? "where" : "which";
const lookupCmd = isWindows() ? "where" : "which";
try {
const stdout = execFileSync(lookupCmd, ["sfw"], {
encoding: "utf8",
Expand Down Expand Up @@ -215,7 +216,7 @@ export async function setupSfw(inputs: Inputs): Promise<boolean> {

async function runDownloadCommand(url: string, outPath: string): Promise<number> {
const options = { ignoreReturnCode: true };
if (process.platform === "win32") {
if (isWindows()) {
return exec(
"pwsh",
[
Expand Down
3 changes: 2 additions & 1 deletion src/run-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { startGroup, endGroup, setFailed, info, warning, error as logError } fro
import { getExecOutput } from "@actions/exec";
import type { Inputs } from "./types.js";
import { getConfiguredProjectDir, getInstallCwd } from "./utils.js";
import { isWindows } from "./ci/platform.js";

const MAX_ERROR_TAIL = 4000;

Expand All @@ -22,7 +23,7 @@ export function isSfwVpNotFoundFlake(stdout: string, stderr: string): boolean {
// 10s-limited resolution runs against warm caches. Best effort: a failure
// here must not block the retry.
async function warmPowerShellCommandCache(): Promise<void> {
if (process.platform !== "win32") return;
if (!isWindows()) return;
try {
await getExecOutput("powershell.exe", ["-NoProfile", "-Command", "Get-Command vp"], {
ignoreReturnCode: true,
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { isAbsolute, join, basename, relative, sep } from "node:path";
import type { Inputs } from "./types.js";
import { LockFileType } from "./types.js";
import type { LockFileInfo } from "./types.js";
import { isWindows } from "./ci/platform.js";

export function getVitePlusHome(): string {
const home = process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME;
const home = isWindows() ? process.env.USERPROFILE : process.env.HOME;
return join(home || homedir(), ".vite-plus");
}

Expand Down
Loading