-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcache-vp.ts
More file actions
83 lines (72 loc) · 2.76 KB
/
cache-vp.ts
File metadata and controls
83 lines (72 loc) · 2.76 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
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";
const SEMVER_RE = /^\d+\.\d+\.\d+/;
/**
* Resolve version input to a precise semver version.
* If the input is already a precise version (e.g. "0.1.8", "1.0.0-beta.1"), return as-is.
* Otherwise treat it as a dist-tag (e.g. "latest", "alpha") and resolve 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) return undefined;
if (SEMVER_RE.test(versionInput)) return versionInput;
try {
const response = await fetch(
`https://registry.npmjs.org/vite-plus/${encodeURIComponent(versionInput)}`,
{ signal: AbortSignal.timeout(10_000) },
);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = (await response.json()) as { version: string };
info(`Resolved vp@${versionInput} to ${data.version}`);
return data.version;
} catch (error) {
warning(`Failed to resolve vp@${versionInput}: ${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)}`);
}
}