-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcache-vp.ts
More file actions
79 lines (69 loc) · 2.51 KB
/
cache-vp.ts
File metadata and controls
79 lines (69 loc) · 2.51 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
import { restoreCache, saveCache } from "@actions/cache";
import { info, debug, saveState, getState, warning } from "@actions/core";
import { arch, platform } from "node:os";
import { State } from "./types.js";
import { getVitePlusHome } from "./utils.js";
/**
* Resolve "latest" to a specific version number via npm registry.
* Returns undefined on failure so the caller can fall back to installing without cache.
*/
export async function resolveVersion(versionInput: string): Promise<string | undefined> {
if (versionInput && versionInput !== "latest") {
return versionInput;
}
try {
const response = await fetch("https://registry.npmjs.org/vite-plus/latest", {
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = (await response.json()) as { version: string };
info(`Resolved latest vp version: ${data.version}`);
return data.version;
} catch (error) {
warning(`Failed to resolve latest vp version: ${error}. Skipping vp cache.`);
return undefined;
}
}
export async function restoreVpCache(version: string, nodeVersion: string): Promise<boolean> {
const vpHome = getVitePlusHome();
const runnerOS = process.env.RUNNER_OS || platform();
const runnerArch = arch();
const primaryKey = `setup-vp-${runnerOS}-${runnerArch}-${version}-node${nodeVersion}`;
debug(`Vp cache key: ${primaryKey}`);
debug(`Vp cache path: ${vpHome}`);
saveState(State.VpCachePrimaryKey, primaryKey);
try {
const matchedKey = await restoreCache([vpHome], primaryKey);
if (matchedKey) {
info(`Vite+ restored from cache (key: ${matchedKey})`);
saveState(State.VpCacheMatchedKey, matchedKey);
return true;
}
} catch (error) {
warning(`Failed to restore vp cache: ${error}`);
}
return false;
}
export async function saveVpCache(): Promise<void> {
const primaryKey = getState(State.VpCachePrimaryKey);
const matchedKey = getState(State.VpCacheMatchedKey);
if (!primaryKey) {
debug("No vp cache key found. Skipping save.");
return;
}
if (primaryKey === matchedKey) {
info(`Vp cache hit on primary key "${primaryKey}". Skipping save.`);
return;
}
try {
const vpHome = getVitePlusHome();
const cacheId = await saveCache([vpHome], primaryKey);
if (cacheId === -1) {
warning("Vp cache save failed or was skipped.");
return;
}
info(`Vp cache saved with key: ${primaryKey}`);
} catch (error) {
warning(`Failed to save vp cache: ${String(error)}`);
}
}