Skip to content

Commit

Permalink
feat: add option for independent Yarn version retrieval (#194)
Browse files Browse the repository at this point in the history
* refactor: move the `getYarnVersion` function to `src/yarn/version.ts`

* feat: add `options.corepack` option to `getYarnVersion` function
  • Loading branch information
threeal authored Feb 25, 2024
1 parent a68c5cb commit 1812e76
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 44 deletions.
34 changes: 23 additions & 11 deletions dist/index.js

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

5 changes: 4 additions & 1 deletion src/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ describe("get cache key", () => {
return false;
});

jest.mocked(getYarnVersion).mockResolvedValue("1.2.3");
jest.mocked(getYarnVersion).mockImplementation(async (options) => {
if (options.corepack) return "1.2.3";
throw new Error("Unable to get Yarn version");
});
});

it("should failed to get Yarn version", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function getCacheKey(): Promise<string> {

core.info("Getting Yarn version...");
try {
const version = await getYarnVersion();
const version = await getYarnVersion({ corepack: true });
cacheKey += `-${version}`;
} catch (err) {
core.setFailed(`Failed to get Yarn version: ${err.message}`);
Expand Down
24 changes: 0 additions & 24 deletions src/yarn/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,3 @@ it("should get Yarn config", async () => {

expect(value).toEqual("/.yarn/berry");
});

it("should get Yarn version", async () => {
const { getExecOutput } = await import("@actions/exec");
const { getYarnVersion } = await import("./index.js");

jest.mocked(getExecOutput).mockResolvedValueOnce({
exitCode: 0,
stdout: "1.2.3",
stderr: "",
});

const version = await getYarnVersion();

expect(getExecOutput).toHaveBeenCalledTimes(1);
expect(getExecOutput).toHaveBeenCalledWith(
"corepack",
["yarn", "--version"],
{
silent: true,
},
);

expect(version).toEqual("1.2.3");
});
8 changes: 1 addition & 7 deletions src/yarn/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exec, getExecOutput } from "@actions/exec";
export { yarnInstall } from "./install.js";
export { getYarnVersion } from "./version.js";

export async function enableYarn(): Promise<void> {
await exec("corepack", ["enable", "yarn"], { silent: true });
Expand All @@ -15,10 +16,3 @@ export async function getYarnConfig(name: string): Promise<string> {
);
return JSON.parse(res.stdout).effective;
}

export async function getYarnVersion() {
const res = await getExecOutput("corepack", ["yarn", "--version"], {
silent: true,
});
return res.stdout.trim();
}
49 changes: 49 additions & 0 deletions src/yarn/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { jest } from "@jest/globals";

jest.unstable_mockModule("@actions/exec", () => ({
getExecOutput: jest.fn(),
}));

describe("get Yarn version", () => {
beforeEach(async () => {
const { getExecOutput } = await import("@actions/exec");

jest.mocked(getExecOutput).mockReset().mockResolvedValueOnce({
exitCode: 0,
stdout: "1.2.3",
stderr: "",
});
});

it("should get Yarn version", async () => {
const { getExecOutput } = await import("@actions/exec");
const { getYarnVersion } = await import("./version.js");

const version = await getYarnVersion();

expect(getExecOutput).toHaveBeenCalledTimes(1);
expect(getExecOutput).toHaveBeenCalledWith("yarn", ["--version"], {
silent: true,
});

expect(version).toEqual("1.2.3");
});

it("should get Yarn version using Corepack", async () => {
const { getExecOutput } = await import("@actions/exec");
const { getYarnVersion } = await import("./version.js");

const version = await getYarnVersion({ corepack: true });

expect(getExecOutput).toHaveBeenCalledTimes(1);
expect(getExecOutput).toHaveBeenCalledWith(
"corepack",
["yarn", "--version"],
{
silent: true,
},
);

expect(version).toEqual("1.2.3");
});
});
18 changes: 18 additions & 0 deletions src/yarn/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getExecOutput } from "@actions/exec";

/**
* Get the current Yarn version.
*
* @param options.corepack - Whether to get the current Yarn version using Corepack or not.
* @returns A promise resolving to the current Yarn version.
*/
export async function getYarnVersion(options?: {
corepack: boolean;
}): Promise<string> {
const commandLine = options?.corepack ? "corepack" : "yarn";
const args = options?.corepack ? ["yarn", "--version"] : ["--version"];
const res = await getExecOutput(commandLine, args, {
silent: true,
});
return res.stdout.trim();
}

0 comments on commit 1812e76

Please sign in to comment.