Skip to content

Commit

Permalink
feat: support running jsr i without package arguments (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Mar 20, 2024
1 parent d0ae63c commit 671c7ee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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"
Expand All @@ -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") {
Expand Down
29 changes: 16 additions & 13 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 19 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,25 @@ describe("install", () => {
}
});

it("jsr i - runs '<pkg-manager> 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/[email protected] - forces npm", async () => {
await withTempEnv(
["i", "--npm", "@std/[email protected]"],
Expand Down

0 comments on commit 671c7ee

Please sign in to comment.