Skip to content

Commit

Permalink
feat: allow getting cache key if lock file does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Feb 19, 2024
1 parent 4fdd5c8 commit 3324c78
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 57 deletions.
50 changes: 24 additions & 26 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ describe("Getting the cache key", () => {
);
});

it("should not get the cache key if there is no lock file", async () => {
it("should get the cache key if there is no lock file", async () => {
const { getCacheKey } = await import("./cache.js");

mock.fs.existsSync.mockReturnValue(false);

const cacheKey = await getCacheKey();

expect(cacheKey).toBeUndefined();
expect(cacheKey).toBe(`yarn-install-action-${os.type()}-1.2.3`);
});
});

Expand Down
16 changes: 10 additions & 6 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import fs from "node:fs";
import os from "node:os";
import { getYarnConfig, getYarnVersion } from "./yarn.js";

export async function getCacheKey(): Promise<string | undefined> {
export async function getCacheKey(): Promise<string> {
core.info("Getting Yarn version...");
const version = await getYarnVersion();

core.info("Calculating lock file hash...");
if (!fs.existsSync("yarn.lock")) {
core.warning(`Lock file not found, skipping cache`);
return undefined;
let lockFileHash: string | undefined = undefined;
if (fs.existsSync("yarn.lock")) {
lockFileHash = await hashFile("yarn.lock", { algorithm: "md5" });
} else {
core.warning(`Lock file could not be found, using empty hash`);
}
const lockFileHash = await hashFile("yarn.lock", { algorithm: "md5" });

const cacheKey = `yarn-install-action-${os.type()}-${version}-${lockFileHash}`;
let cacheKey = `yarn-install-action-${os.type()}-${version}`;
if (lockFileHash !== undefined) {
cacheKey += `-${lockFileHash}`;
}
core.info(`Using cache key: ${cacheKey}`);

return cacheKey;
Expand Down
38 changes: 15 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,29 @@ async function main(): Promise<void> {
});

const cacheKey = await core.group("Getting cache key", getCacheKey);
const cachePaths = await core.group("Getting cache paths", getCachePaths);

let cachePaths: string[] = [];
if (cacheKey !== undefined) {
cachePaths = await core.group("Getting cache paths", getCachePaths);
}

if (cacheKey !== undefined) {
const cacheFound = await core.group("Restoring cache", async () => {
const cacheId = await cache.restoreCache(cachePaths.slice(), cacheKey);
if (cacheId === undefined) {
core.warning("Cache not found");
return false;
}
return true;
});

if (cacheFound) {
core.info("Cache restored successfully");
return;
const cacheFound = await core.group("Restoring cache", async () => {
const cacheId = await cache.restoreCache(cachePaths.slice(), cacheKey);
if (cacheId === undefined) {
core.warning("Cache not found");
return false;
}
return true;
});

if (cacheFound) {
core.info("Cache restored successfully");
return;
}

await core.group("Installing dependencies", async () => {
return yarnInstall();
});

if (cacheKey !== undefined) {
await core.group("Saving cache", async () => {
return cache.saveCache(cachePaths.slice(), cacheKey);
});
}
await core.group("Saving cache", async () => {
return cache.saveCache(cachePaths.slice(), cacheKey);
});
}

main().catch((err) => core.setFailed(err));

0 comments on commit 3324c78

Please sign in to comment.