Skip to content

Commit

Permalink
chore(create-cloudflare): add support for new bun.lock (#6831)
Browse files Browse the repository at this point in the history
* chore: add support for new bun.lock

* chore: changeset

* Add tests

---------

Co-authored-by: Samuel Macleod <[email protected]>
  • Loading branch information
Cherry and penalosa authored Nov 18, 2024
1 parent bb17205 commit 0fe018c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-mayflies-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cloudflare": patch
---

Adds support for new upcoming bun.lock lockfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { existsSync } from "fs";
import { detectPackageManager } from "helpers/packageManagers";
import {
detectPackageManager,
detectPmMismatch,
} from "helpers/packageManagers";
import { beforeEach, describe, expect, test, vi } from "vitest";
import whichPMRuns from "which-pm-runs";
import { mockPackageManager } from "./mocks";
import type { C3Context } from "types";

vi.mock("fs");
vi.mock("which-pm-runs");
Expand Down Expand Up @@ -71,4 +75,66 @@ describe("Package Managers", () => {
expect(pm.dlx).toEqual(["yarn"]);
});
});

describe("detectPmMismatch", async () => {
describe("pnpm", () => {
beforeEach(() => {
mockPackageManager("pnpm");
});

test.each([
["yarn.lock", true],
["pnpm-lock.yaml", false],
["bun.lock", true],
["bun.lockb", true],
])("with %s", (file, isMismatch) => {
vi.mocked(existsSync).mockImplementationOnce(
(path) => !!(path as string).includes(file),
);
expect(detectPmMismatch({ project: { path: "" } } as C3Context)).toBe(
isMismatch,
);
});
});

describe("yarn", () => {
beforeEach(() => {
mockPackageManager("yarn");
});

test.each([
["yarn.lock", false],
["pnpm-lock.yaml", true],
["bun.lock", true],
["bun.lockb", true],
])("with %s", (file, isMismatch) => {
vi.mocked(existsSync).mockImplementationOnce(
(path) => !!(path as string).includes(file),
);
expect(detectPmMismatch({ project: { path: "" } } as C3Context)).toBe(
isMismatch,
);
});
});

describe("bun", () => {
beforeEach(() => {
mockPackageManager("bun");
});

test.each([
["yarn.lock", true],
["pnpm-lock.yaml", true],
["bun.lock", false],
["bun.lockb", false],
])("with %s", (file, isMismatch) => {
vi.mocked(existsSync).mockImplementationOnce(
(path) => !!(path as string).includes(file),
);
expect(detectPmMismatch({ project: { path: "" } } as C3Context)).toBe(
isMismatch,
);
});
});
});
});
7 changes: 5 additions & 2 deletions packages/create-cloudflare/src/helpers/packageManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const rectifyPmMismatch = async (ctx: C3Context) => {
});
};

const detectPmMismatch = (ctx: C3Context) => {
export const detectPmMismatch = (ctx: C3Context) => {
const { npm } = detectPackageManager();
const projectPath = ctx.project.path;

Expand All @@ -131,6 +131,9 @@ const detectPmMismatch = (ctx: C3Context) => {
case "pnpm":
return !existsSync(path.join(projectPath, "pnpm-lock.yaml"));
case "bun":
return !existsSync(path.join(projectPath, "bun.lockb"));
return (
!existsSync(path.join(projectPath, "bun.lockb")) &&
!existsSync(path.join(projectPath, "bun.lock"))
);
}
};

0 comments on commit 0fe018c

Please sign in to comment.