@@ -2,27 +2,23 @@ const crypto = require('crypto')
22const fs = require ( 'fs' )
33const path = require ( 'path' )
44const core = require ( '@actions/core' )
5- const config = require ( './config' )
65
7- const diskCachePath = config . diskCache . paths [ 0 ]
8- const diskCacheHash = diskCachePath + '.sha256'
9-
10- function init ( ) {
11- if ( ! config . diskCache . enabled ) {
6+ function init ( cacheConfig ) {
7+ if ( ! cacheConfig . enabled ) {
128 return
139 }
1410
15- core . startGroup ( " Computing initial disk cache hash" )
16- fs . writeFileSync ( diskCacheHash , computeDiskCacheHash ( ) )
11+ core . startGroup ( ` Computing initial ${ cacheConfig . name } cache hash` )
12+ fs . writeFileSync ( cacheConfig . path + '.sha256' , computeCacheHash ( cacheConfig . path ) )
1713 core . endGroup ( )
1814}
1915
20- function run ( ) {
21- if ( ! fs . existsSync ( diskCachePath ) ) {
16+ function run ( cacheConfig ) {
17+ if ( ! fs . existsSync ( cacheConfig . path ) ) {
2218 return
2319 }
2420
25- const files = fs . readdirSync ( diskCachePath , { withFileTypes : true , recursive : true } )
21+ const files = fs . readdirSync ( cacheConfig . path , { withFileTypes : true , recursive : true } )
2622 . filter ( d => d . isFile ( ) )
2723 . map ( d => {
2824 const file = path . join ( d . path , d . name )
@@ -31,8 +27,8 @@ function run() {
3127 } )
3228 . sort ( ( a , b ) => b . mtime - a . mtime )
3329
34- core . startGroup ( `Running disk cache garbage collection` )
35- const deleteThreshold = config . maxDiskCacheSize * 1024 ** 3
30+ core . startGroup ( `Running ${ cacheConfig . name } cache garbage collection` )
31+ const deleteThreshold = cacheConfig . maxSize * 1024 ** 3
3632 let cacheSize = 0
3733 let reclaimed = 0
3834 for ( const { file, size } of files ) {
@@ -44,21 +40,24 @@ function run() {
4440 }
4541 core . info ( `Reclaimed ${ reclaimed } files` )
4642 core . endGroup ( )
43+
44+ return cacheChanged ( cacheConfig )
4745}
4846
49- function cacheChanged ( ) {
50- core . startGroup ( `Checking disk cache for changes` )
51- const changed = fs . readFileSync ( diskCacheHash ) != computeDiskCacheHash ( )
47+ function cacheChanged ( cacheConfig ) {
48+ core . startGroup ( `Checking ${ cacheConfig . name } cache for changes` )
49+ const hash = computeCacheHash ( cacheConfig . path )
50+ const changed = fs . readFileSync ( cacheConfig . path + '.sha256' ) != hash
5251 core . info ( `Cache has changes: ${ changed } ` )
5352 core . endGroup ( )
54- return changed
53+ return changed ? hash : undefined
5554}
5655
57- function computeDiskCacheHash ( ) {
56+ function computeCacheHash ( path ) {
5857 let hash = crypto . createHash ( 'sha256' )
5958
60- if ( fs . existsSync ( diskCachePath ) ) {
61- const files = fs . readdirSync ( diskCachePath , { withFileTypes : true , recursive : true } )
59+ if ( fs . existsSync ( path ) ) {
60+ const files = fs . readdirSync ( path , { withFileTypes : true , recursive : true } )
6261 . filter ( d => d . isFile ( ) )
6362 . map ( d => d . path )
6463 . sort ( )
@@ -74,5 +73,4 @@ function computeDiskCacheHash() {
7473module . exports = {
7574 init,
7675 run,
77- cacheChanged,
7876}
0 commit comments