-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (71 loc) · 2.61 KB
/
index.ts
File metadata and controls
84 lines (71 loc) · 2.61 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
72
73
74
75
76
77
78
79
80
81
82
83
84
import { saveState, getState, setFailed, info, setOutput, warning } from "@actions/core";
import { exec, getExecOutput } from "@actions/exec";
import { getInputs } from "./inputs.js";
import { installVitePlus } from "./install-viteplus.js";
import { runViteInstall } from "./run-install.js";
import { restoreCache } from "./cache-restore.js";
import { saveCache } from "./cache-save.js";
import { saveVpCache } from "./cache-vp.js";
import { State, Outputs } from "./types.js";
import type { Inputs } from "./types.js";
import { resolveNodeVersionFile } from "./node-version-file.js";
async function runMain(inputs: Inputs): Promise<void> {
// Mark that post action should run
saveState(State.IsPost, "true");
// Step 1: Resolve Node.js version (needed for cache key)
let nodeVersion = inputs.nodeVersion;
if (!nodeVersion && inputs.nodeVersionFile) {
nodeVersion = resolveNodeVersionFile(inputs.nodeVersionFile);
}
// Step 2: Install Vite+ (with cache keyed by vp version + node version)
await installVitePlus(inputs, nodeVersion || "");
// Step 3: Set up Node.js version if specified
if (nodeVersion) {
info(`Setting up Node.js ${nodeVersion} via vp env use...`);
await exec("vp", ["env", "use", nodeVersion]);
}
// Step 4: Restore cache if enabled
if (inputs.cache) {
await restoreCache(inputs);
}
// Step 5: Run vp install if requested
if (inputs.runInstall.length > 0) {
await runViteInstall(inputs);
}
// Print version info at the end
await printViteVersion();
}
async function printViteVersion(): Promise<void> {
try {
const result = await getExecOutput("vp", ["--version"], { silent: true });
const versionOutput = result.stdout.trim();
info(versionOutput);
// Extract global version for output (e.g., "- Global: v0.0.0" -> "0.0.0")
const globalMatch = versionOutput.match(/Global:\s*v?([\d.]+[^\s]*)/i);
const version = globalMatch?.[1] || "unknown";
saveState(State.InstalledVersion, version);
setOutput(Outputs.Version, version);
} catch (error) {
warning(`Could not get vp version: ${String(error)}`);
setOutput(Outputs.Version, "unknown");
}
}
async function runPost(inputs: Inputs): Promise<void> {
const saves: Promise<void>[] = [saveVpCache()];
if (inputs.cache) {
saves.push(saveCache());
}
await Promise.all(saves);
}
async function main(): Promise<void> {
const inputs = getInputs();
if (getState(State.IsPost) === "true") {
await runPost(inputs);
} else {
await runMain(inputs);
}
}
main().catch((error) => {
console.error(error);
setFailed(error instanceof Error ? error.message : String(error));
});