Skip to content

Commit 0209631

Browse files
authored
fix: prefer bun.lockd over yarn.lock (#34)
* test: add test for `findProjectDir()` * fix: prefer bun.lockd over yarn.lock * style: format deno fmt
1 parent 4b53471 commit 0209631

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ export async function findProjectDir(
9090
return result;
9191
}
9292

93+
// prefer bun.lockb over yarn.lock
94+
// In some cases, both bun.lockb and yarn.lock can exist in the same project.
95+
// https://bun.sh/docs/install/lockfile
96+
const bunLockfile = path.join(dir, "bun.lockb");
97+
if (await fileExists(bunLockfile)) {
98+
logDebug(`Detected bun from lockfile ${bunLockfile}`);
99+
result.projectDir = dir;
100+
result.pkgManagerName = "bun";
101+
return result;
102+
}
103+
93104
const yarnLockFile = path.join(dir, "yarn.lock");
94105
if (await fileExists(yarnLockFile)) {
95106
logDebug(`Detected yarn from lockfile ${yarnLockFile}`);
@@ -106,14 +117,6 @@ export async function findProjectDir(
106117
return result;
107118
}
108119

109-
const bunLockfile = path.join(dir, "bun.lockb");
110-
if (await fileExists(bunLockfile)) {
111-
logDebug(`Detected bun from lockfile ${bunLockfile}`);
112-
result.projectDir = dir;
113-
result.pkgManagerName = "bun";
114-
return result;
115-
}
116-
117120
const pkgJsonPath = path.join(dir, "package.json");
118121
if (await fileExists(pkgJsonPath)) {
119122
logDebug(`Found package.json at ${pkgJsonPath}`);

test/utils.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as assert from "assert/strict";
2+
import * as fs from "fs";
3+
import * as path from "node:path";
4+
import { runInTempDir } from "./test_utils";
5+
import { findProjectDir } from "../src/utils";
6+
7+
describe("findProjectDir", () => {
8+
it("should return npm if package-lock.json is found", async () => {
9+
await runInTempDir(async (tempDir) => {
10+
await fs.promises.writeFile(
11+
path.join(tempDir, "package-lock.json"),
12+
"{}",
13+
"utf-8",
14+
);
15+
const result = await findProjectDir(tempDir);
16+
assert.strictEqual(result.pkgManagerName, "npm");
17+
});
18+
});
19+
it("should return yarn if yarn.lock is found", async () => {
20+
await runInTempDir(async (tempDir) => {
21+
await fs.promises.writeFile(path.join(tempDir, "yarn.lock"), "", "utf-8");
22+
const result = await findProjectDir(tempDir);
23+
assert.strictEqual(result.pkgManagerName, "yarn");
24+
});
25+
});
26+
it("should return pnpm if pnpm-lock.yaml is found", async () => {
27+
await runInTempDir(async (tempDir) => {
28+
await fs.promises.writeFile(
29+
path.join(tempDir, "pnpm-lock.yaml"),
30+
"",
31+
"utf-8",
32+
);
33+
const result = await findProjectDir(tempDir);
34+
assert.strictEqual(result.pkgManagerName, "pnpm");
35+
});
36+
});
37+
it("should return bun if bun.lockb is found", async () => {
38+
await runInTempDir(async (tempDir) => {
39+
await fs.promises.writeFile(path.join(tempDir, "bun.lockb"), "", "utf-8");
40+
const result = await findProjectDir(tempDir);
41+
assert.strictEqual(result.pkgManagerName, "bun");
42+
});
43+
});
44+
it("should return bun if bun.lockb and yarn.lock are found", async () => {
45+
// bun allow to save bun.lockb and yarn.lock
46+
// https://bun.sh/docs/install/lockfile
47+
await runInTempDir(async (tempDir) => {
48+
await fs.promises.writeFile(path.join(tempDir, "bun.lockb"), "", "utf-8");
49+
await fs.promises.writeFile(path.join(tempDir, "yarn.lock"), "", "utf-8");
50+
const result = await findProjectDir(tempDir);
51+
assert.strictEqual(result.pkgManagerName, "bun");
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)