Skip to content

Commit

Permalink
fix: wrong projectDir in workspace setup (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Mar 4, 2024
1 parent 7fb628d commit fa08e2f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export async function findProjectDir(
const npmLockfile = path.join(dir, "package-lock.json");
if (await fileExists(npmLockfile)) {
logDebug(`Detected npm from lockfile ${npmLockfile}`);
result.projectDir = dir;
result.pkgManagerName = "npm";
return result;
}
Expand All @@ -96,31 +95,31 @@ export async function findProjectDir(
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}`);
result.projectDir = dir;
result.pkgManagerName = "yarn";
return result;
}

const pnpmLockfile = path.join(dir, "pnpm-lock.yaml");
if (await fileExists(pnpmLockfile)) {
logDebug(`Detected pnpm from lockfile ${pnpmLockfile}`);
result.projectDir = dir;
result.pkgManagerName = "pnpm";
return result;
}

const pkgJsonPath = path.join(dir, "package.json");
if (await fileExists(pkgJsonPath)) {
logDebug(`Found package.json at ${pkgJsonPath}`);
result.projectDir = dir;
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;
Expand Down
31 changes: 31 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ describe("install", () => {
});
});

it("jsr i @std/encoding - adds to nearest package.json", async () => {
await runInTempDir(async (dir) => {
const parentPkgJson = { name: "", private: true };
await writeJson(path.join(dir, "package.json"), parentPkgJson);

// Create sub folder with package.json
await fs.promises.mkdir(path.join(dir, "sub"));
await writeJson(path.join(dir, "sub", "package.json"), { name: "foo" });

await runJsr(["i", "@std/encoding"], path.join(dir, "sub"));

assert.deepEqual(
await readJson(path.join(dir, "package.json")),
parentPkgJson,
);

const pkgJson = await readJson<PkgJson>(
path.join(dir, "sub", "package.json"),
);
assert.ok(
pkgJson.dependencies && "@std/encoding" in pkgJson.dependencies,
"Missing dependency entry",
);

assert.match(
pkgJson.dependencies["@std/encoding"],
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
);
});
});

it("jsr i @std/[email protected] - with version", async () => {
await withTempEnv(["i", "@std/[email protected]"], async (getPkgJson) => {
const pkgJson = await getPkgJson();
Expand Down
18 changes: 17 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from "assert/strict";
import * as fs from "fs";
import * as path from "node:path";
import { runInTempDir } from "./test_utils";
import { runInTempDir, writeJson } from "./test_utils";
import { findProjectDir } from "../src/utils";

describe("findProjectDir", () => {
Expand All @@ -16,13 +16,15 @@ describe("findProjectDir", () => {
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(
Expand All @@ -34,13 +36,15 @@ describe("findProjectDir", () => {
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
Expand All @@ -51,4 +55,16 @@ describe("findProjectDir", () => {
assert.strictEqual(result.pkgManagerName, "bun");
});
});

it("should set project dir to nearest package.json", async () => {
await runInTempDir(async (tempDir) => {
const sub = path.join(tempDir, "sub");
await fs.promises.mkdir(sub);

await writeJson(path.join(tempDir, "package.json"), {});
await writeJson(path.join(sub, "package.json"), {});
const result = await findProjectDir(sub);
assert.strictEqual(result.projectDir, sub);
});
});
});

0 comments on commit fa08e2f

Please sign in to comment.