Skip to content

Commit

Permalink
fix: wrong project folder detection (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Mar 11, 2024
1 parent 25124b8 commit 20d5f1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ export async function findProjectDir(
pkgJsonPath: null,
},
): Promise<ProjectInfo> {
// Ensure we check for `package.json` first as this defines
// the root project location.
if (result.pkgJsonPath === null) {
const pkgJsonPath = path.join(dir, "package.json");
if (await fileExists(pkgJsonPath)) {
logDebug(`Found package.json at ${pkgJsonPath}`);
logDebug(`Setting project directory to ${dir}`);
result.projectDir = dir;
result.pkgJsonPath = pkgJsonPath;
}
}

const npmLockfile = path.join(dir, "package-lock.json");
if (await fileExists(npmLockfile)) {
logDebug(`Detected npm from lockfile ${npmLockfile}`);
Expand Down Expand Up @@ -113,15 +125,6 @@ export async function findProjectDir(
return result;
}

if (result.pkgJsonPath === null) {
const pkgJsonPath = path.join(dir, "package.json");
if (await fileExists(pkgJsonPath)) {
logDebug(`Found package.json at ${pkgJsonPath}`);
result.projectDir = dir;
result.pkgJsonPath = pkgJsonPath;
}
}

const prev = dir;
dir = path.dirname(dir);
if (dir === prev) {
Expand Down
18 changes: 18 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "./test_utils";
import * as assert from "node:assert/strict";
import {
exec,
PkgJson,
readJson,
readTextFile,
Expand Down Expand Up @@ -289,6 +290,23 @@ describe("install", () => {
);
});

it("pnpm install into existing project", async () => {
await runInTempDir(async (dir) => {
const sub = path.join(dir, "sub", "sub1");
await fs.promises.mkdir(sub, {
recursive: true,
});

await exec("pnpm", ["i", "preact"], dir);

await runJsr(["i", "--pnpm", "@std/[email protected]"], sub);
assert.ok(
await isFile(path.join(dir, "pnpm-lock.yaml")),
"pnpm lockfile not created",
);
});
});

if (process.platform !== "win32") {
it("jsr add --bun @std/[email protected] - forces bun", async () => {
await withTempEnv(
Expand Down

0 comments on commit 20d5f1e

Please sign in to comment.