Skip to content

Commit

Permalink
feat: Add npmrc support check for Bun package manager (fixes: #93)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ryoppippi committed Jul 4, 2024
1 parent b507e9c commit 97d4293
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/pkg_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")}`));
Expand Down Expand Up @@ -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";
Expand Down
28 changes: 24 additions & 4 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as kl from "kolorist";
import {
DenoJson,
enableYarnBerry,
isBunSupportNpmrc,
isDirectory,
isFile,
runInTempDir,
Expand Down Expand Up @@ -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");
}
},
);
});
Expand All @@ -453,8 +463,18 @@ describe("install", () => {
["i", "--bun", "@std/[email protected]"],
async (dir) => {
await runJsr(["i", "--bun", "@std/[email protected]"], 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");
}
},
);
});
Expand Down
7 changes: 7 additions & 0 deletions test/test_utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<void>) {
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "jsr-cli"));

Expand Down

0 comments on commit 97d4293

Please sign in to comment.