Skip to content

Commit 2166dcc

Browse files
committed
@actions/cache: abstract @actions/cache
In a similar way to @actions/core, we abstract the @actions/cache functions. Signed-off-by: Matthew John Cheetham <[email protected]>
1 parent 958f054 commit 2166dcc

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

dist/index.js

Lines changed: 28 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {ICore} from './src/core'
22
import {ActionsCore} from './src/actions_core'
33
import {mkdirp} from './src/downloader'
4-
import {restoreCache, saveCache} from '@actions/cache'
54
import process from 'process'
65
import {spawnSync} from 'child_process'
76
import {
@@ -67,9 +66,13 @@ async function setup(
6766
useCache = false
6867
}
6968

69+
if (!core.isCacheAvailable()) {
70+
useCache = false
71+
}
72+
7073
let needToDownload = true
7174
try {
72-
if (useCache && (await restoreCache([outputDirectory], id))) {
75+
if (useCache && (await core.restoreCache([outputDirectory], id))) {
7376
core.info(`Cached ${id} was successfully restored`)
7477
needToDownload = false
7578
}
@@ -86,7 +89,7 @@ async function setup(
8689
)
8790

8891
try {
89-
if (useCache && !(await saveCache([outputDirectory], id))) {
92+
if (useCache && !(await core.saveCache([outputDirectory], id))) {
9093
core.warning(`Failed to cache ${id}`)
9194
}
9295
} catch (e) {

src/actions_core.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import {ICore} from '../src/core'
22
import * as core from '@actions/core'
3+
import * as cache from '@actions/cache'
34

45
export class ActionsCore implements ICore {
6+
isCacheAvailable(): boolean {
7+
return cache.isFeatureAvailable()
8+
}
9+
10+
async restoreCache(
11+
paths: string[],
12+
primaryKey: string
13+
): Promise<string | undefined> {
14+
return cache.restoreCache(paths, primaryKey)
15+
}
16+
17+
async saveCache(paths: string[], key: string): Promise<number> {
18+
return cache.saveCache(paths, key)
19+
}
20+
521
getInput(name: string): string {
622
return core.getInput(name)
723
}

src/core.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
* This allows us to avoid module shimming and provide clean implementations for different platforms.
44
*/
55
export interface ICore {
6+
/**
7+
* Checks if caching is available.
8+
*/
9+
isCacheAvailable(): boolean
10+
11+
/**
12+
* Restores cache from keys
13+
*
14+
* @param paths a list of file paths to restore from the cache
15+
* @param primaryKey an explicit key for restoring the cache. Lookup is done with prefix matching.
16+
* @returns string returns the key for the cache hit, otherwise returns undefined
17+
*/
18+
restoreCache(paths: string[], primaryKey: string): Promise<string | undefined>
19+
20+
/**
21+
* Saves a list of files with the specified key
22+
*
23+
* @param paths a list of file paths to be cached
24+
* @param key an explicit key for restoring the cache
25+
* @returns number returns cacheId if the cache was saved successfully and throws an error if save fails
26+
*/
27+
saveCache(paths: string[], key: string): Promise<number>
28+
629
/**
730
* Gets an input from the action/task configuration.
831
* @param name The name of the input

0 commit comments

Comments
 (0)