From 62a8f25abf26933a335f07ab791c86efd1e9a64f Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Thu, 22 Jun 2023 17:07:37 +0200 Subject: [PATCH] Use cache prefix if all sub-projects are yarn managed --- dist/cache-save/index.js | 17 +++++++---------- dist/setup/index.js | 19 ++++++++----------- src/cache-restore.ts | 5 ++++- src/cache-utils.ts | 19 ++++++++----------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index 4f0a1b62..adcf2d4b 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -60604,23 +60604,20 @@ const isCacheManagedByYarn3 = (directory) => __awaiter(void 0, void 0, void 0, f return enableGlobalCache === 'false'; }); /** - * A function to report either the repo contains at least one Yarn managed directory + * A function to report the repo contains Yarn managed projects * @param packageManagerInfo - used to make sure current package manager is yarn - * @return - true if there's at least one Yarn managed directory in the repo + * @param cacheDependencyPath - either a single string or multiline string with possible glob patterns + * expected to be the result of `core.getInput('cache-dependency-path')` + * @return - true if all project directories configured to be Yarn managed */ -const repoHasYarn3ManagedCache = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () { +const repoHasYarn3ManagedCache = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { if (packageManagerInfo.name !== 'yarn') return false; - const cacheDependencyPath = core.getInput('cache-dependency-path'); const yarnDirs = cacheDependencyPath ? yield getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath) : ['']; - for (const dir of yarnDirs.length === 0 ? [''] : yarnDirs) { - if (yield isCacheManagedByYarn3(dir)) { - return true; - } - } - return false; + const isManagedList = yield Promise.all(yarnDirs.map(isCacheManagedByYarn3)); + return isManagedList.every(Boolean); }); exports.repoHasYarn3ManagedCache = repoHasYarn3ManagedCache; function isGhes() { diff --git a/dist/setup/index.js b/dist/setup/index.js index fb63a807..0bbe9052 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71157,7 +71157,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, const primaryKey = `${keyPrefix}-${fileHash}`; core.debug(`primary key is ${primaryKey}`); core.saveState(constants_1.State.CachePrimaryKey, primaryKey); - const cacheKey = (yield cache_utils_1.repoHasYarn3ManagedCache(packageManagerInfo)) + const cacheKey = (yield cache_utils_1.repoHasYarn3ManagedCache(packageManagerInfo, cacheDependencyPath)) ? yield cache.restoreCache(cachePaths, primaryKey, [keyPrefix]) : yield cache.restoreCache(cachePaths, primaryKey); core.setOutput('cache-hit', Boolean(cacheKey)); @@ -71390,23 +71390,20 @@ const isCacheManagedByYarn3 = (directory) => __awaiter(void 0, void 0, void 0, f return enableGlobalCache === 'false'; }); /** - * A function to report either the repo contains at least one Yarn managed directory + * A function to report the repo contains Yarn managed projects * @param packageManagerInfo - used to make sure current package manager is yarn - * @return - true if there's at least one Yarn managed directory in the repo + * @param cacheDependencyPath - either a single string or multiline string with possible glob patterns + * expected to be the result of `core.getInput('cache-dependency-path')` + * @return - true if all project directories configured to be Yarn managed */ -const repoHasYarn3ManagedCache = (packageManagerInfo) => __awaiter(void 0, void 0, void 0, function* () { +const repoHasYarn3ManagedCache = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { if (packageManagerInfo.name !== 'yarn') return false; - const cacheDependencyPath = core.getInput('cache-dependency-path'); const yarnDirs = cacheDependencyPath ? yield getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath) : ['']; - for (const dir of yarnDirs.length === 0 ? [''] : yarnDirs) { - if (yield isCacheManagedByYarn3(dir)) { - return true; - } - } - return false; + const isManagedList = yield Promise.all(yarnDirs.map(isCacheManagedByYarn3)); + return isManagedList.every(Boolean); }); exports.repoHasYarn3ManagedCache = repoHasYarn3ManagedCache; function isGhes() { diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 73d08010..3342ddc3 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -44,7 +44,10 @@ export const restoreCache = async ( core.saveState(State.CachePrimaryKey, primaryKey); - const cacheKey = (await repoHasYarn3ManagedCache(packageManagerInfo)) + const cacheKey = (await repoHasYarn3ManagedCache( + packageManagerInfo, + cacheDependencyPath + )) ? await cache.restoreCache(cachePaths, primaryKey, [keyPrefix]) : await cache.restoreCache(cachePaths, primaryKey); diff --git a/src/cache-utils.ts b/src/cache-utils.ts index c2466f3b..c7f55670 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -249,28 +249,25 @@ const isCacheManagedByYarn3 = async (directory: string): Promise => { }; /** - * A function to report either the repo contains at least one Yarn managed directory + * A function to report the repo contains Yarn managed projects * @param packageManagerInfo - used to make sure current package manager is yarn - * @return - true if there's at least one Yarn managed directory in the repo + * @param cacheDependencyPath - either a single string or multiline string with possible glob patterns + * expected to be the result of `core.getInput('cache-dependency-path')` + * @return - true if all project directories configured to be Yarn managed */ export const repoHasYarn3ManagedCache = async ( - packageManagerInfo: PackageManagerInfo + packageManagerInfo: PackageManagerInfo, + cacheDependencyPath: string ): Promise => { if (packageManagerInfo.name !== 'yarn') return false; - const cacheDependencyPath = core.getInput('cache-dependency-path'); - const yarnDirs = cacheDependencyPath ? await getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath) : ['']; - for (const dir of yarnDirs.length === 0 ? [''] : yarnDirs) { - if (await isCacheManagedByYarn3(dir)) { - return true; - } - } + const isManagedList = await Promise.all(yarnDirs.map(isCacheManagedByYarn3)); - return false; + return isManagedList.every(Boolean); }; export function isGhes(): boolean {