Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong project folder detection #57

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading