-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinstall-viteplus.ts
More file actions
71 lines (62 loc) · 2.29 KB
/
install-viteplus.ts
File metadata and controls
71 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
info(`Installing ${DISPLAY_NAME}@${version}...`);
// 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,
} 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") {
return exec(
"pwsh",
["-Command", `& ([scriptblock]::Create((irm ${INSTALL_URL_PS1})))`],
options,
);
}
return exec("bash", ["-c", `curl -fsSL ${INSTALL_URL_SH} | bash`], options);
}
function ensureVitePlusBinInPath(): void {
const binDir = join(getVitePlusHome(), "bin");
if (!process.env.PATH?.includes(binDir)) {
addPath(binDir);
}
}