Skip to content

Commit fa08e2f

Browse files
fix: wrong projectDir in workspace setup (#40)
1 parent 7fb628d commit fa08e2f

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/utils.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ export async function findProjectDir(
8585
const npmLockfile = path.join(dir, "package-lock.json");
8686
if (await fileExists(npmLockfile)) {
8787
logDebug(`Detected npm from lockfile ${npmLockfile}`);
88-
result.projectDir = dir;
8988
result.pkgManagerName = "npm";
9089
return result;
9190
}
@@ -96,31 +95,31 @@ export async function findProjectDir(
9695
const bunLockfile = path.join(dir, "bun.lockb");
9796
if (await fileExists(bunLockfile)) {
9897
logDebug(`Detected bun from lockfile ${bunLockfile}`);
99-
result.projectDir = dir;
10098
result.pkgManagerName = "bun";
10199
return result;
102100
}
103101

104102
const yarnLockFile = path.join(dir, "yarn.lock");
105103
if (await fileExists(yarnLockFile)) {
106104
logDebug(`Detected yarn from lockfile ${yarnLockFile}`);
107-
result.projectDir = dir;
108105
result.pkgManagerName = "yarn";
109106
return result;
110107
}
111108

112109
const pnpmLockfile = path.join(dir, "pnpm-lock.yaml");
113110
if (await fileExists(pnpmLockfile)) {
114111
logDebug(`Detected pnpm from lockfile ${pnpmLockfile}`);
115-
result.projectDir = dir;
116112
result.pkgManagerName = "pnpm";
117113
return result;
118114
}
119115

120-
const pkgJsonPath = path.join(dir, "package.json");
121-
if (await fileExists(pkgJsonPath)) {
122-
logDebug(`Found package.json at ${pkgJsonPath}`);
123-
result.projectDir = dir;
116+
if (result.pkgJsonPath === null) {
117+
const pkgJsonPath = path.join(dir, "package.json");
118+
if (await fileExists(pkgJsonPath)) {
119+
logDebug(`Found package.json at ${pkgJsonPath}`);
120+
result.projectDir = dir;
121+
result.pkgJsonPath = pkgJsonPath;
122+
}
124123
}
125124

126125
const prev = dir;

test/commands.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@ describe("install", () => {
6565
});
6666
});
6767

68+
it("jsr i @std/encoding - adds to nearest package.json", async () => {
69+
await runInTempDir(async (dir) => {
70+
const parentPkgJson = { name: "", private: true };
71+
await writeJson(path.join(dir, "package.json"), parentPkgJson);
72+
73+
// Create sub folder with package.json
74+
await fs.promises.mkdir(path.join(dir, "sub"));
75+
await writeJson(path.join(dir, "sub", "package.json"), { name: "foo" });
76+
77+
await runJsr(["i", "@std/encoding"], path.join(dir, "sub"));
78+
79+
assert.deepEqual(
80+
await readJson(path.join(dir, "package.json")),
81+
parentPkgJson,
82+
);
83+
84+
const pkgJson = await readJson<PkgJson>(
85+
path.join(dir, "sub", "package.json"),
86+
);
87+
assert.ok(
88+
pkgJson.dependencies && "@std/encoding" in pkgJson.dependencies,
89+
"Missing dependency entry",
90+
);
91+
92+
assert.match(
93+
pkgJson.dependencies["@std/encoding"],
94+
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
95+
);
96+
});
97+
});
98+
6899
it("jsr i @std/encoding@0.216.0 - with version", async () => {
69100
await withTempEnv(["i", "@std/encoding@0.216.0"], async (getPkgJson) => {
70101
const pkgJson = await getPkgJson();

test/utils.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from "assert/strict";
22
import * as fs from "fs";
33
import * as path from "node:path";
4-
import { runInTempDir } from "./test_utils";
4+
import { runInTempDir, writeJson } from "./test_utils";
55
import { findProjectDir } from "../src/utils";
66

77
describe("findProjectDir", () => {
@@ -16,13 +16,15 @@ describe("findProjectDir", () => {
1616
assert.strictEqual(result.pkgManagerName, "npm");
1717
});
1818
});
19+
1920
it("should return yarn if yarn.lock is found", async () => {
2021
await runInTempDir(async (tempDir) => {
2122
await fs.promises.writeFile(path.join(tempDir, "yarn.lock"), "", "utf-8");
2223
const result = await findProjectDir(tempDir);
2324
assert.strictEqual(result.pkgManagerName, "yarn");
2425
});
2526
});
27+
2628
it("should return pnpm if pnpm-lock.yaml is found", async () => {
2729
await runInTempDir(async (tempDir) => {
2830
await fs.promises.writeFile(
@@ -34,13 +36,15 @@ describe("findProjectDir", () => {
3436
assert.strictEqual(result.pkgManagerName, "pnpm");
3537
});
3638
});
39+
3740
it("should return bun if bun.lockb is found", async () => {
3841
await runInTempDir(async (tempDir) => {
3942
await fs.promises.writeFile(path.join(tempDir, "bun.lockb"), "", "utf-8");
4043
const result = await findProjectDir(tempDir);
4144
assert.strictEqual(result.pkgManagerName, "bun");
4245
});
4346
});
47+
4448
it("should return bun if bun.lockb and yarn.lock are found", async () => {
4549
// bun allow to save bun.lockb and yarn.lock
4650
// https://bun.sh/docs/install/lockfile
@@ -51,4 +55,16 @@ describe("findProjectDir", () => {
5155
assert.strictEqual(result.pkgManagerName, "bun");
5256
});
5357
});
58+
59+
it("should set project dir to nearest package.json", async () => {
60+
await runInTempDir(async (tempDir) => {
61+
const sub = path.join(tempDir, "sub");
62+
await fs.promises.mkdir(sub);
63+
64+
await writeJson(path.join(tempDir, "package.json"), {});
65+
await writeJson(path.join(sub, "package.json"), {});
66+
const result = await findProjectDir(sub);
67+
assert.strictEqual(result.projectDir, sub);
68+
});
69+
});
5470
});

0 commit comments

Comments
 (0)