|
1 | | -import { restoreCache as restoreCacheAction } from '@actions/cache' |
2 | | -import { hashFiles } from '@actions/glob' |
3 | | -import { warning, info, debug, saveState, setOutput } from '@actions/core' |
4 | | -import { arch, platform } from 'node:os' |
5 | | -import type { Inputs } from './types.js' |
6 | | -import { State, Outputs } from './types.js' |
7 | | -import { detectLockFile, getCacheDirectories } from './utils.js' |
| 1 | +import { restoreCache as restoreCacheAction } from "@actions/cache"; |
| 2 | +import { hashFiles } from "@actions/glob"; |
| 3 | +import { warning, info, debug, saveState, setOutput } from "@actions/core"; |
| 4 | +import { arch, platform } from "node:os"; |
| 5 | +import type { Inputs } from "./types.js"; |
| 6 | +import { State, Outputs } from "./types.js"; |
| 7 | +import { detectLockFile, getCacheDirectories } from "./utils.js"; |
8 | 8 |
|
9 | 9 | export async function restoreCache(inputs: Inputs): Promise<void> { |
10 | 10 | // Detect lock file |
11 | | - const lockFile = detectLockFile(inputs.cacheDependencyPath) |
| 11 | + const lockFile = detectLockFile(inputs.cacheDependencyPath); |
12 | 12 | if (!lockFile) { |
13 | | - warning('No lock file found. Skipping cache restore.') |
14 | | - setOutput(Outputs.CacheHit, false) |
15 | | - return |
| 13 | + warning("No lock file found. Skipping cache restore."); |
| 14 | + setOutput(Outputs.CacheHit, false); |
| 15 | + return; |
16 | 16 | } |
17 | 17 |
|
18 | | - info(`Using lock file: ${lockFile.path}`) |
| 18 | + info(`Using lock file: ${lockFile.path}`); |
19 | 19 |
|
20 | 20 | // Get cache directories based on lock file type |
21 | | - const cachePaths = await getCacheDirectories(lockFile.type) |
| 21 | + const cachePaths = await getCacheDirectories(lockFile.type); |
22 | 22 | if (!cachePaths.length) { |
23 | | - warning('No cache directories found. Skipping cache restore.') |
24 | | - setOutput(Outputs.CacheHit, false) |
25 | | - return |
| 23 | + warning("No cache directories found. Skipping cache restore."); |
| 24 | + setOutput(Outputs.CacheHit, false); |
| 25 | + return; |
26 | 26 | } |
27 | 27 |
|
28 | | - debug(`Cache paths: ${cachePaths.join(', ')}`) |
29 | | - saveState(State.CachePaths, JSON.stringify(cachePaths)) |
| 28 | + debug(`Cache paths: ${cachePaths.join(", ")}`); |
| 29 | + saveState(State.CachePaths, JSON.stringify(cachePaths)); |
30 | 30 |
|
31 | 31 | // Generate cache key: vite-plus-{platform}-{arch}-{lockfile-type}-{hash} |
32 | | - const runnerOS = process.env.RUNNER_OS || platform() |
33 | | - const runnerArch = arch() |
34 | | - const fileHash = await hashFiles(lockFile.path) |
| 32 | + const runnerOS = process.env.RUNNER_OS || platform(); |
| 33 | + const runnerArch = arch(); |
| 34 | + const fileHash = await hashFiles(lockFile.path); |
35 | 35 |
|
36 | 36 | if (!fileHash) { |
37 | | - throw new Error(`Failed to generate hash for lock file: ${lockFile.path}`) |
| 37 | + throw new Error(`Failed to generate hash for lock file: ${lockFile.path}`); |
38 | 38 | } |
39 | 39 |
|
40 | | - const primaryKey = `vite-plus-${runnerOS}-${runnerArch}-${lockFile.type}-${fileHash}` |
| 40 | + const primaryKey = `vite-plus-${runnerOS}-${runnerArch}-${lockFile.type}-${fileHash}`; |
41 | 41 | const restoreKeys = [ |
42 | 42 | `vite-plus-${runnerOS}-${runnerArch}-${lockFile.type}-`, |
43 | 43 | `vite-plus-${runnerOS}-${runnerArch}-`, |
44 | | - ] |
| 44 | + ]; |
45 | 45 |
|
46 | | - debug(`Primary key: ${primaryKey}`) |
47 | | - debug(`Restore keys: ${restoreKeys.join(', ')}`) |
| 46 | + debug(`Primary key: ${primaryKey}`); |
| 47 | + debug(`Restore keys: ${restoreKeys.join(", ")}`); |
48 | 48 |
|
49 | | - saveState(State.CachePrimaryKey, primaryKey) |
| 49 | + saveState(State.CachePrimaryKey, primaryKey); |
50 | 50 |
|
51 | 51 | // Attempt to restore cache |
52 | | - const matchedKey = await restoreCacheAction( |
53 | | - cachePaths, |
54 | | - primaryKey, |
55 | | - restoreKeys |
56 | | - ) |
| 52 | + const matchedKey = await restoreCacheAction(cachePaths, primaryKey, restoreKeys); |
57 | 53 |
|
58 | 54 | if (matchedKey) { |
59 | | - info(`Cache restored from key: ${matchedKey}`) |
60 | | - saveState(State.CacheMatchedKey, matchedKey) |
61 | | - setOutput(Outputs.CacheHit, true) |
| 55 | + info(`Cache restored from key: ${matchedKey}`); |
| 56 | + saveState(State.CacheMatchedKey, matchedKey); |
| 57 | + setOutput(Outputs.CacheHit, true); |
62 | 58 | } else { |
63 | | - info('Cache not found') |
64 | | - setOutput(Outputs.CacheHit, false) |
| 59 | + info("Cache not found"); |
| 60 | + setOutput(Outputs.CacheHit, false); |
65 | 61 | } |
66 | 62 | } |
0 commit comments