Skip to content

Commit

Permalink
feat: Add npmrc support check for Bun package manager (#94)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ryoppippi and marvinhagemeister authored Jul 5, 2024
1 parent 8f33b74 commit f38b56a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 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
8 changes: 8 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 @@ -181,6 +182,13 @@ export class Bun implements PackageManager {
async runScript(script: string) {
await execWithLog("bun", ["run", script], this.cwd);
}

async isNpmrcSupported() {
const output = await exec("bun", ["--version"], this.cwd, undefined, true);
const version = output.stdout;
// 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
31 changes: 27 additions & 4 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
writeJson,
writeTextFile,
} from "../src/utils";
import { Bun } from "../src/pkg_manager";

describe("general", () => {
it("exit 1 on unknown command", async () => {
Expand Down Expand Up @@ -443,8 +444,19 @@ describe("install", () => {
"bun lockfile not created",
);

const config = await readTextFile(path.join(dir, "bunfig.toml"));
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
const bun = new Bun(dir);

if (await bun.isNpmrcSupported()) {
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 +465,19 @@ 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");

const bun = new Bun(dir);
if (await bun.isNpmrcSupported()) {
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
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"outDir": "dist/",
"declaration": true,
"sourceMap": true,
"isolatedModules": true
"isolatedModules": true,
"esModuleInterop": true
},
"include": ["src"]
}

0 comments on commit f38b56a

Please sign in to comment.