From 578e73af1643feeb5d4077650db96b5cbc55394d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 12 Jun 2024 14:55:45 +0900 Subject: [PATCH 1/2] feat: add 'setup' subcommand --- src/bin.ts | 5 +++++ src/commands.ts | 49 ++++++++++++++++++++++++++++++------------- test/commands.test.ts | 41 +++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 16 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 04e6d61..f28db2a 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -9,6 +9,7 @@ import { publish, remove, runScript, + setup, showPackageInfo, } from "./commands"; import { @@ -238,6 +239,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)) { diff --git a/src/commands.ts b/src/commands.ts index d51171c..83a7721 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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) { } } +/** Sets up `@jsr` scope to map it to npm.jsr.io registry for various package managers. */ +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(), diff --git a/test/commands.test.ts b/test/commands.test.ts index 6c58702..28a5e2e 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -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"); + }); + }); +}); From e64523f196d57e4ed508ee0b80cdf00f280744d5 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 12 Jun 2024 15:05:25 +0900 Subject: [PATCH 2/2] document --- README.md | 2 ++ src/bin.ts | 4 ++++ src/commands.ts | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6c5e69b..25ba295 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ commands. - `publish`: Publish `package.json` libraries to JSR. - `run