From 97d4293241f99377522db988ca2c4b04d5e58d9e Mon Sep 17 00:00:00 2001 From: ryoppippi <1560508+ryoppippi@users.noreply.github.com> Date: Thu, 4 Jul 2024 10:21:38 +0100 Subject: [PATCH] feat: Add npmrc support check for Bun package manager (fixes: #93) This commit introduces a check to determine if the Bun package manager supports npmrc. If the version of Bun is 1.1.18 or higher, it is considered to support npmrc. This information is used to decide whether to create a bunfig.toml file or not. The tests have been updated accordingly to check for either bunfig.toml or .npmrc based on the version of Bun. --- src/commands.ts | 6 ++++-- src/pkg_manager.ts | 7 +++++++ test/commands.test.ts | 28 ++++++++++++++++++++++++---- test/test_utils.ts | 7 +++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index d51171c..233ba5f 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -94,8 +94,10 @@ export async function install(packages: JsrPackage[], options: InstallOptions) { ); if (packages.length > 0) { - if (pkgManager instanceof Bun) { - // Bun doesn't support reading from .npmrc yet + if (pkgManager instanceof Bun && !(await pkgManager.isNpmrcSupported())) { + // Bun v1.1.17 or lower doesn't support reading from .npmrc + // Bun v1.1.18+ supports npmrc + // https://bun.sh/blog/bun-v1.1.18#npmrc-support await setupBunfigToml(root); } else if (pkgManager instanceof YarnBerry) { // Yarn v2+ does not read from .npmrc intentionally diff --git a/src/pkg_manager.ts b/src/pkg_manager.ts index 7580ebf..088ad00 100644 --- a/src/pkg_manager.ts +++ b/src/pkg_manager.ts @@ -3,6 +3,7 @@ import { getLatestPackageVersion } from "./api"; import { InstallOptions } from "./commands"; import { exec, findProjectDir, JsrPackage, logDebug } from "./utils"; import * as kl from "kolorist"; +import semiver from "semiver"; async function execWithLog(cmd: string, args: string[], cwd: string) { console.log(kl.dim(`$ ${cmd} ${args.join(" ")}`)); @@ -180,6 +181,12 @@ export class Bun implements PackageManager { async runScript(script: string) { await execWithLog("bun", ["run", script], this.cwd); } + + async isNpmrcSupported() { + const version = await exec("bun", ["--version"], this.cwd, undefined, true); + // bun v1.1.18 supports npmrc https://bun.sh/blog/bun-v1.1.18#npmrc-support + return version != null && semiver(version, "1.1.18") >= 0; + } } export type PkgManagerName = "npm" | "yarn" | "pnpm" | "bun"; diff --git a/test/commands.test.ts b/test/commands.test.ts index 6c58702..d753188 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -4,6 +4,7 @@ import * as kl from "kolorist"; import { DenoJson, enableYarnBerry, + isBunSupportNpmrc, isDirectory, isFile, runInTempDir, @@ -443,8 +444,17 @@ describe("install", () => { "bun lockfile not created", ); - const config = await readTextFile(path.join(dir, "bunfig.toml")); - assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created"); + if (await isBunSupportNpmrc(dir)) { + const npmrcPath = path.join(dir, ".npmrc"); + const npmRc = await readTextFile(npmrcPath); + assert.ok( + npmRc.includes("@jsr:registry=https://npm.jsr.io"), + "Missing npmrc registry", + ); + } else { + const config = await readTextFile(path.join(dir, "bunfig.toml")); + assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created"); + } }, ); }); @@ -453,8 +463,18 @@ describe("install", () => { ["i", "--bun", "@std/encoding@0.216.0"], async (dir) => { await runJsr(["i", "--bun", "@std/encoding@0.216.0"], dir); - const config = await readTextFile(path.join(dir, "bunfig.toml")); - assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created"); + + if (await isBunSupportNpmrc(dir)) { + const npmrcPath = path.join(dir, ".npmrc"); + const npmRc = await readTextFile(npmrcPath); + assert.ok( + npmRc.includes("@jsr:registry=https://npm.jsr.io"), + "Missing npmrc registry", + ); + } else { + const config = await readTextFile(path.join(dir, "bunfig.toml")); + assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created"); + } }, ); }); diff --git a/test/test_utils.ts b/test/test_utils.ts index 14067f9..8b71d71 100644 --- a/test/test_utils.ts +++ b/test/test_utils.ts @@ -1,6 +1,7 @@ import * as fs from "node:fs"; import * as path from "node:path"; import * as os from "node:os"; +import semiver from 'semiver'; import { exec, writeJson } from "../src/utils"; export interface DenoJson { @@ -32,6 +33,12 @@ export async function runJsr( }, captureOutput); } +export async function isBunSupportNpmrc(cwd: string){ + const version = await exec("bun", ["--version"], cwd, undefined, true); + // bun v1.1.18 supports npmrc https://bun.sh/blog/bun-v1.1.18#npmrc-support + return version != null && semiver(version, "1.1.18") >= 0; +} + export async function runInTempDir(fn: (dir: string) => Promise) { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "jsr-cli"));