Skip to content

feat: add jsr setup subcommand #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ commands.
- `publish`: Publish `package.json` libraries to JSR.
- `run <script>`: Run a JSR package script.
- `<script>`: Run a JSR package script without `run` command.
- `setup`: Set up the project for JSR. Add the settings for mapping @jsr scope
to JSR registry.

## Limitations

9 changes: 9 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {
publish,
remove,
runScript,
setup,
showPackageInfo,
} from "./commands";
import {
@@ -58,6 +59,10 @@ ${
["r, uninstall, remove", "Remove one or more JSR packages."],
["publish", "Publish a package to the JSR registry."],
["info, show, view", "Show package information."],
[
"setup",
"Set up the project for JSR. Add the settings for mapping @jsr scope to JSR registry.",
],
])
}
@@ -238,6 +243,10 @@ if (args.length === 0) {
run(async () => {
await runScript(process.cwd(), script, { pkgManagerName });
});
} else if (cmd === "setup") {
run(async () => {
await setup({ pkgManagerName });
});
} else {
const packageJsonPath = path.join(process.cwd(), "package.json");
if (fs.existsSync(packageJsonPath)) {
49 changes: 34 additions & 15 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,13 @@ import {
JsrPackage,
timeAgo,
} from "./utils";
import { Bun, getPkgManager, PkgManagerName, YarnBerry } from "./pkg_manager";
import {
Bun,
getPkgManager,
PackageManager,
PkgManagerName,
YarnBerry,
} from "./pkg_manager";
import { downloadDeno, getDenoDownloadUrl } from "./download";
import { getNpmPackageInfo, getPackageMeta } from "./api";
import semiver from "semiver";
@@ -79,6 +85,23 @@ export async function setupBunfigToml(dir: string) {
}
}

/** Adds settings to map `@jsr` scope to https://npm.jsr.io registry for the given package manager */
async function setupJsrScope(dir: string, pkgManager: PackageManager) {
if (pkgManager instanceof Bun) {
// Bun doesn't support reading from .npmrc yet
await setupBunfigToml(dir);
} else if (pkgManager instanceof YarnBerry) {
// Yarn v2+ does not read from .npmrc intentionally
// https://yarnpkg.com/migration/guide#update-your-configuration-to-the-new-settings
await pkgManager.setConfigValue(
JSR_YARN_BERRY_CONFIG_KEY,
JSR_NPM_REGISTRY_URL,
);
} else {
await setupNpmRc(dir);
}
}

export interface BaseOptions {
pkgManagerName: PkgManagerName | null;
}
@@ -94,26 +117,22 @@ export async function install(packages: JsrPackage[], options: InstallOptions) {
);

if (packages.length > 0) {
if (pkgManager instanceof Bun) {
// Bun doesn't support reading from .npmrc yet
await setupBunfigToml(root);
} else if (pkgManager instanceof YarnBerry) {
// Yarn v2+ does not read from .npmrc intentionally
// https://yarnpkg.com/migration/guide#update-your-configuration-to-the-new-settings
await pkgManager.setConfigValue(
JSR_YARN_BERRY_CONFIG_KEY,
JSR_NPM_REGISTRY_URL,
);
} else {
await setupNpmRc(root);
}

await setupJsrScope(root, pkgManager);
console.log(`Installing ${kl.cyan(packages.join(", "))}...`);
}

await pkgManager.install(packages, options);
}

export async function setup(options: BaseOptions) {
const { pkgManager, root } = await getPkgManager(
process.cwd(),
options.pkgManagerName,
);

await setupJsrScope(root, pkgManager);
}

export async function remove(packages: JsrPackage[], options: BaseOptions) {
const { pkgManager } = await getPkgManager(
process.cwd(),
41 changes: 40 additions & 1 deletion test/commands.test.ts
Original file line number Diff line number Diff line change
@@ -196,7 +196,7 @@ describe("install", () => {

assert.match(
pkgJson.dependencies["@std/encoding"],
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
/^npm:@jsr\/std__encoding@\^?\d+\.\d+\.\d+.*$/,
);

const npmRc = await readTextFile(path.join(dir, ".npmrc"));
@@ -801,3 +801,42 @@ describe("show", () => {
assert.ok(txt.includes("npm tarball:"));
});
});

describe("setup", () => {
it("jsr setup - should generate .npmrc file", async () => {
await runInTempDir(async (dir) => {
await runJsr(["setup"], 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",
);
});
});

it("jsr setup - should generate .yarnrc.yml if yarn berry is detected", async () => {
await runInTempDir(async (dir) => {
await enableYarnBerry(dir);
await writeTextFile(path.join(dir, "yarn.lock"), "");

await runJsr(["setup"], dir);

const yarnRc = await readTextFile(path.join(dir, ".yarnrc.yml"));
assert.ok(
/jsr:\s*npmRegistryServer: "https:\/\/npm\.jsr\.io"/.test(yarnRc),
"Missing yarnrc.yml registry",
);
});
});

it("jsr setup - should generate bunfig.toml if bun is detected", async () => {
await runInTempDir(async (dir) => {
await runJsr(["setup", "--bun"], dir);

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