Skip to content

Commit 671c7ee

Browse files
feat: support running jsr i without package arguments (#67)
1 parent d0ae63c commit 671c7ee

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

src/bin.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ ${
112112
`);
113113
}
114114

115-
function getPackages(positionals: string[]): JsrPackage[] {
115+
function getPackages(positionals: string[], allowEmpty: boolean): JsrPackage[] {
116116
const pkgArgs = positionals.slice(1);
117117
const packages = pkgArgs.map((p) => JsrPackage.from(p));
118118

119-
if (pkgArgs.length === 0) {
119+
if (!allowEmpty && pkgArgs.length === 0) {
120120
console.error(kl.red(`Missing packages argument.`));
121121
console.log();
122122
printHelp();
@@ -211,7 +211,8 @@ if (args.length === 0) {
211211

212212
if (cmd === "i" || cmd === "install" || cmd === "add") {
213213
run(async () => {
214-
const packages = getPackages(options.positionals);
214+
const packages = getPackages(options.positionals, true);
215+
215216
await install(packages, {
216217
mode: options.values["save-dev"]
217218
? "dev"
@@ -223,7 +224,7 @@ if (args.length === 0) {
223224
});
224225
} else if (cmd === "r" || cmd === "uninstall" || cmd === "remove") {
225226
run(async () => {
226-
const packages = getPackages(options.positionals);
227+
const packages = getPackages(options.positionals, false);
227228
await remove(packages, { pkgManagerName });
228229
});
229230
} else if (cmd === "run") {

src/commands.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,24 @@ export interface InstallOptions extends BaseOptions {
8989
export async function install(packages: JsrPackage[], options: InstallOptions) {
9090
const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName);
9191

92-
if (pkgManager instanceof Bun) {
93-
// Bun doesn't support reading from .npmrc yet
94-
await setupBunfigToml(pkgManager.cwd);
95-
} else if (pkgManager instanceof YarnBerry) {
96-
// Yarn v2+ does not read from .npmrc intentionally
97-
// https://yarnpkg.com/migration/guide#update-your-configuration-to-the-new-settings
98-
await pkgManager.setConfigValue(
99-
JSR_YARN_BERRY_CONFIG_KEY,
100-
JSR_NPM_REGISTRY_URL,
101-
);
102-
} else {
103-
await setupNpmRc(pkgManager.cwd);
92+
if (packages.length > 0) {
93+
if (pkgManager instanceof Bun) {
94+
// Bun doesn't support reading from .npmrc yet
95+
await setupBunfigToml(pkgManager.cwd);
96+
} else if (pkgManager instanceof YarnBerry) {
97+
// Yarn v2+ does not read from .npmrc intentionally
98+
// https://yarnpkg.com/migration/guide#update-your-configuration-to-the-new-settings
99+
await pkgManager.setConfigValue(
100+
JSR_YARN_BERRY_CONFIG_KEY,
101+
JSR_NPM_REGISTRY_URL,
102+
);
103+
} else {
104+
await setupNpmRc(pkgManager.cwd);
105+
}
106+
107+
console.log(`Installing ${kl.cyan(packages.join(", "))}...`);
104108
}
105109

106-
console.log(`Installing ${kl.cyan(packages.join(", "))}...`);
107110
await pkgManager.install(packages, options);
108111
}
109112

test/commands.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ describe("install", () => {
240240
}
241241
});
242242

243+
it("jsr i - runs '<pkg-manager> install' instead", async () => {
244+
await runInTempDir(async (dir) => {
245+
await runJsr(["i", "@std/encoding"], dir);
246+
247+
assert.ok(
248+
fs.existsSync(path.join(dir, "node_modules")),
249+
"No node_modules created.",
250+
);
251+
252+
await fs.promises.rm(path.join(dir, "node_modules"), { recursive: true });
253+
254+
await runJsr(["i"], dir);
255+
assert.ok(
256+
fs.existsSync(path.join(dir, "node_modules")),
257+
"No node_modules created.",
258+
);
259+
});
260+
});
261+
243262
it("jsr add --npm @std/[email protected] - forces npm", async () => {
244263
await withTempEnv(
245264
["i", "--npm", "@std/[email protected]"],

0 commit comments

Comments
 (0)