diff --git a/src/bin.ts b/src/bin.ts index 3a25dd8..9167cea 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -112,11 +112,11 @@ ${ `); } -function getPackages(positionals: string[]): JsrPackage[] { +function getPackages(positionals: string[], allowEmpty: boolean): JsrPackage[] { const pkgArgs = positionals.slice(1); const packages = pkgArgs.map((p) => JsrPackage.from(p)); - if (pkgArgs.length === 0) { + if (!allowEmpty && pkgArgs.length === 0) { console.error(kl.red(`Missing packages argument.`)); console.log(); printHelp(); @@ -211,7 +211,8 @@ if (args.length === 0) { if (cmd === "i" || cmd === "install" || cmd === "add") { run(async () => { - const packages = getPackages(options.positionals); + const packages = getPackages(options.positionals, true); + await install(packages, { mode: options.values["save-dev"] ? "dev" @@ -223,7 +224,7 @@ if (args.length === 0) { }); } else if (cmd === "r" || cmd === "uninstall" || cmd === "remove") { run(async () => { - const packages = getPackages(options.positionals); + const packages = getPackages(options.positionals, false); await remove(packages, { pkgManagerName }); }); } else if (cmd === "run") { diff --git a/src/commands.ts b/src/commands.ts index 8d441b6..2a02043 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -89,21 +89,24 @@ export interface InstallOptions extends BaseOptions { export async function install(packages: JsrPackage[], options: InstallOptions) { const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName); - if (pkgManager instanceof Bun) { - // Bun doesn't support reading from .npmrc yet - await setupBunfigToml(pkgManager.cwd); - } 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(pkgManager.cwd); + if (packages.length > 0) { + if (pkgManager instanceof Bun) { + // Bun doesn't support reading from .npmrc yet + await setupBunfigToml(pkgManager.cwd); + } 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(pkgManager.cwd); + } + + console.log(`Installing ${kl.cyan(packages.join(", "))}...`); } - console.log(`Installing ${kl.cyan(packages.join(", "))}...`); await pkgManager.install(packages, options); } diff --git a/test/commands.test.ts b/test/commands.test.ts index 6ba0fe3..0eb0eef 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -240,6 +240,25 @@ describe("install", () => { } }); + it("jsr i - runs ' install' instead", async () => { + await runInTempDir(async (dir) => { + await runJsr(["i", "@std/encoding"], dir); + + assert.ok( + fs.existsSync(path.join(dir, "node_modules")), + "No node_modules created.", + ); + + await fs.promises.rm(path.join(dir, "node_modules"), { recursive: true }); + + await runJsr(["i"], dir); + assert.ok( + fs.existsSync(path.join(dir, "node_modules")), + "No node_modules created.", + ); + }); + }); + it("jsr add --npm @std/encoding@0.216.0 - forces npm", async () => { await withTempEnv( ["i", "--npm", "@std/encoding@0.216.0"],