Skip to content

Commit

Permalink
fix: prefer bun.lockd over yarn.lock (#34)
Browse files Browse the repository at this point in the history
* test: add test for `findProjectDir()`

* fix: prefer bun.lockd over yarn.lock

* style: format deno fmt
  • Loading branch information
azu authored Mar 2, 2024
1 parent 4b53471 commit 0209631
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ export async function findProjectDir(
return result;
}

// prefer bun.lockb over yarn.lock
// In some cases, both bun.lockb and yarn.lock can exist in the same project.
// https://bun.sh/docs/install/lockfile
const bunLockfile = path.join(dir, "bun.lockb");
if (await fileExists(bunLockfile)) {
logDebug(`Detected bun from lockfile ${bunLockfile}`);
result.projectDir = dir;
result.pkgManagerName = "bun";
return result;
}

const yarnLockFile = path.join(dir, "yarn.lock");
if (await fileExists(yarnLockFile)) {
logDebug(`Detected yarn from lockfile ${yarnLockFile}`);
Expand All @@ -106,14 +117,6 @@ export async function findProjectDir(
return result;
}

const bunLockfile = path.join(dir, "bun.lockb");
if (await fileExists(bunLockfile)) {
logDebug(`Detected bun from lockfile ${bunLockfile}`);
result.projectDir = dir;
result.pkgManagerName = "bun";
return result;
}

const pkgJsonPath = path.join(dir, "package.json");
if (await fileExists(pkgJsonPath)) {
logDebug(`Found package.json at ${pkgJsonPath}`);
Expand Down
54 changes: 54 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as assert from "assert/strict";
import * as fs from "fs";
import * as path from "node:path";
import { runInTempDir } from "./test_utils";
import { findProjectDir } from "../src/utils";

describe("findProjectDir", () => {
it("should return npm if package-lock.json is found", async () => {
await runInTempDir(async (tempDir) => {
await fs.promises.writeFile(
path.join(tempDir, "package-lock.json"),
"{}",
"utf-8",
);
const result = await findProjectDir(tempDir);
assert.strictEqual(result.pkgManagerName, "npm");
});
});
it("should return yarn if yarn.lock is found", async () => {
await runInTempDir(async (tempDir) => {
await fs.promises.writeFile(path.join(tempDir, "yarn.lock"), "", "utf-8");
const result = await findProjectDir(tempDir);
assert.strictEqual(result.pkgManagerName, "yarn");
});
});
it("should return pnpm if pnpm-lock.yaml is found", async () => {
await runInTempDir(async (tempDir) => {
await fs.promises.writeFile(
path.join(tempDir, "pnpm-lock.yaml"),
"",
"utf-8",
);
const result = await findProjectDir(tempDir);
assert.strictEqual(result.pkgManagerName, "pnpm");
});
});
it("should return bun if bun.lockb is found", async () => {
await runInTempDir(async (tempDir) => {
await fs.promises.writeFile(path.join(tempDir, "bun.lockb"), "", "utf-8");
const result = await findProjectDir(tempDir);
assert.strictEqual(result.pkgManagerName, "bun");
});
});
it("should return bun if bun.lockb and yarn.lock are found", async () => {
// bun allow to save bun.lockb and yarn.lock
// https://bun.sh/docs/install/lockfile
await runInTempDir(async (tempDir) => {
await fs.promises.writeFile(path.join(tempDir, "bun.lockb"), "", "utf-8");
await fs.promises.writeFile(path.join(tempDir, "yarn.lock"), "", "utf-8");
const result = await findProjectDir(tempDir);
assert.strictEqual(result.pkgManagerName, "bun");
});
});
});

0 comments on commit 0209631

Please sign in to comment.