diff --git a/src/utils.test.ts b/src/utils.test.ts index cfe65cf..6c2604a 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,6 +1,11 @@ -import { expect, test, describe } from "vitest"; -import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils"; import path from "node:path"; +import { describe, expect, test } from "vitest"; +import { + checkWorkingDirectory, + detectPackageManager, + getNpxCmd, + semverCompare, +} from "./utils"; test("getNpxCmd ", async () => { process.env.RUNNER_OS = "Windows"; @@ -43,3 +48,25 @@ describe("checkWorkingDirectory", () => { ); }); }); + +describe("detectPackageManager", () => { + test("should return name of package manager for current workspace", () => { + expect(detectPackageManager()).toBe("npm"); + }); + + test("should return npm if package-lock.json exists", () => { + expect(detectPackageManager("test/fixtures/npm")).toBe("npm"); + }); + + test("should return yarn if yarn.lock exists", () => { + expect(detectPackageManager("test/fixtures/yarn")).toBe("yarn"); + }); + + test("should return pnpm if pnpm-lock.yaml exists", () => { + expect(detectPackageManager("test/fixtures/pnpm")).toBe("pnpm"); + }); + + test("should return null if no package manager is detected", () => { + expect(detectPackageManager("test/fixtures/empty")).toBe(null); + }); +}); diff --git a/src/utils.ts b/src/utils.ts index ab6d0d2..4ce5de7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -33,3 +33,20 @@ export function checkWorkingDirectory(workingDirectory = ".") { throw new Error(`Directory ${workingDirectory} does not exist.`); } } + +export type PackageManager = "npm" | "yarn" | "pnpm"; + +export function detectPackageManager( + workingDirectory = ".", +): PackageManager | null { + if (existsSync(path.join(workingDirectory, "package-lock.json"))) { + return "npm"; + } + if (existsSync(path.join(workingDirectory, "yarn.lock"))) { + return "yarn"; + } + if (existsSync(path.join(workingDirectory, "pnpm-lock.yaml"))) { + return "pnpm"; + } + return null; +} diff --git a/test/fixtures/empty/.gitkeep b/test/fixtures/empty/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/npm/package-lock.json b/test/fixtures/npm/package-lock.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/pnpm/pnpm-lock.yaml b/test/fixtures/pnpm/pnpm-lock.yaml new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/yarn/yarn.lock b/test/fixtures/yarn/yarn.lock new file mode 100644 index 0000000..e69de29