From 0e4b10045891d0b11e4a3431ffe6aefc09b7b2fc Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Fri, 26 Aug 2022 07:04:11 -0600 Subject: [PATCH] Use node to set file timestamps Using `find` and `touch` will not work on windows, so this provides a cross-platform mechanism. --- src/cache-cleaner.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/cache-cleaner.ts b/src/cache-cleaner.ts index 4fef8ac..3a8e536 100644 --- a/src/cache-cleaner.ts +++ b/src/cache-cleaner.ts @@ -1,4 +1,6 @@ +import * as core from '@actions/core' import * as exec from '@actions/exec' +import * as glob from '@actions/glob' import fs from 'fs' import path from 'path' @@ -46,14 +48,21 @@ export class CacheCleaner { } private async ageAllFiles(fileName = '*'): Promise { - await exec.exec( - 'find', - [this.gradleUserHome, '-name', fileName, '-exec', 'touch', '-m', '-t', '197001010000', '{}', '+'], - {} - ) + core.debug(`Aging all files in Gradle User Homee with name ${fileName}`) + await this.setUtimes(`${this.gradleUserHome}/**/${fileName}`, new Date(0)) } private async touchAllFiles(fileName = '*'): Promise { - await exec.exec('find', [this.gradleUserHome, '-name', fileName, '-exec', 'touch', '-m', '{}', '+'], {}) + core.debug(`Touching all files in Gradle User Home with name ${fileName}`) + await this.setUtimes(`${this.gradleUserHome}/**/${fileName}`, new Date()) + } + + private async setUtimes(pattern: string, timestamp: Date): Promise { + const globber = await glob.create(pattern, { + implicitDescendants: false + }) + for await (const file of globber.globGenerator()) { + fs.utimesSync(file, timestamp, timestamp) + } } }