Skip to content

Commit f38b56a

Browse files
feat: Add npmrc support check for Bun package manager (#94)
* 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. --------- Co-authored-by: Marvin Hagemeister <[email protected]>
1 parent 8f33b74 commit f38b56a

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/commands.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ export async function install(packages: JsrPackage[], options: InstallOptions) {
9494
);
9595

9696
if (packages.length > 0) {
97-
if (pkgManager instanceof Bun) {
98-
// Bun doesn't support reading from .npmrc yet
97+
if (pkgManager instanceof Bun && !(await pkgManager.isNpmrcSupported())) {
98+
// Bun v1.1.17 or lower doesn't support reading from .npmrc
99+
// Bun v1.1.18+ supports npmrc
100+
// https://bun.sh/blog/bun-v1.1.18#npmrc-support
99101
await setupBunfigToml(root);
100102
} else if (pkgManager instanceof YarnBerry) {
101103
// Yarn v2+ does not read from .npmrc intentionally

src/pkg_manager.ts

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getLatestPackageVersion } from "./api";
33
import { InstallOptions } from "./commands";
44
import { exec, findProjectDir, JsrPackage, logDebug } from "./utils";
55
import * as kl from "kolorist";
6+
import semiver from "semiver";
67

78
async function execWithLog(cmd: string, args: string[], cwd: string) {
89
console.log(kl.dim(`$ ${cmd} ${args.join(" ")}`));
@@ -181,6 +182,13 @@ export class Bun implements PackageManager {
181182
async runScript(script: string) {
182183
await execWithLog("bun", ["run", script], this.cwd);
183184
}
185+
186+
async isNpmrcSupported() {
187+
const output = await exec("bun", ["--version"], this.cwd, undefined, true);
188+
const version = output.stdout;
189+
// bun v1.1.18 supports npmrc https://bun.sh/blog/bun-v1.1.18#npmrc-support
190+
return version != null && semiver(version, "1.1.18") >= 0;
191+
}
184192
}
185193

186194
export type PkgManagerName = "npm" | "yarn" | "pnpm" | "bun";

test/commands.test.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
writeJson,
2121
writeTextFile,
2222
} from "../src/utils";
23+
import { Bun } from "../src/pkg_manager";
2324

2425
describe("general", () => {
2526
it("exit 1 on unknown command", async () => {
@@ -443,8 +444,19 @@ describe("install", () => {
443444
"bun lockfile not created",
444445
);
445446

446-
const config = await readTextFile(path.join(dir, "bunfig.toml"));
447-
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
447+
const bun = new Bun(dir);
448+
449+
if (await bun.isNpmrcSupported()) {
450+
const npmrcPath = path.join(dir, ".npmrc");
451+
const npmRc = await readTextFile(npmrcPath);
452+
assert.ok(
453+
npmRc.includes("@jsr:registry=https://npm.jsr.io"),
454+
"Missing npmrc registry",
455+
);
456+
} else {
457+
const config = await readTextFile(path.join(dir, "bunfig.toml"));
458+
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
459+
}
448460
},
449461
);
450462
});
@@ -453,8 +465,19 @@ describe("install", () => {
453465
["i", "--bun", "@std/[email protected]"],
454466
async (dir) => {
455467
await runJsr(["i", "--bun", "@std/[email protected]"], dir);
456-
const config = await readTextFile(path.join(dir, "bunfig.toml"));
457-
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
468+
469+
const bun = new Bun(dir);
470+
if (await bun.isNpmrcSupported()) {
471+
const npmrcPath = path.join(dir, ".npmrc");
472+
const npmRc = await readTextFile(npmrcPath);
473+
assert.ok(
474+
npmRc.includes("@jsr:registry=https://npm.jsr.io"),
475+
"Missing npmrc registry",
476+
);
477+
} else {
478+
const config = await readTextFile(path.join(dir, "bunfig.toml"));
479+
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
480+
}
458481
},
459482
);
460483
});

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"outDir": "dist/",
77
"declaration": true,
88
"sourceMap": true,
9-
"isolatedModules": true
9+
"isolatedModules": true,
10+
"esModuleInterop": true
1011
},
1112
"include": ["src"]
1213
}

0 commit comments

Comments
 (0)