From fa08e2fc0990d26c9b1fe8cdc33d9b1eb44da4cb Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 4 Mar 2024 14:46:27 +0100 Subject: [PATCH] fix: wrong projectDir in workspace setup (#40) --- src/utils.ts | 15 +++++++-------- test/commands.test.ts | 31 +++++++++++++++++++++++++++++++ test/utils.test.ts | 18 +++++++++++++++++- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 30ccc55..d0c51c8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; } @@ -96,7 +95,6 @@ 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; } @@ -104,7 +102,6 @@ export async function findProjectDir( 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; } @@ -112,15 +109,17 @@ export async function findProjectDir( 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; diff --git a/test/commands.test.ts b/test/commands.test.ts index dacdc12..2811bd2 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -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( + 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/encoding@0.216.0 - with version", async () => { await withTempEnv(["i", "@std/encoding@0.216.0"], async (getPkgJson) => { const pkgJson = await getPkgJson(); diff --git a/test/utils.test.ts b/test/utils.test.ts index 098e214..190d4c1 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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", () => { @@ -16,6 +16,7 @@ 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"); @@ -23,6 +24,7 @@ describe("findProjectDir", () => { assert.strictEqual(result.pkgManagerName, "yarn"); }); }); + it("should return pnpm if pnpm-lock.yaml is found", async () => { await runInTempDir(async (tempDir) => { await fs.promises.writeFile( @@ -34,6 +36,7 @@ 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"); @@ -41,6 +44,7 @@ describe("findProjectDir", () => { 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 @@ -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); + }); + }); });