Skip to content

Commit 2d616c4

Browse files
committed
pkgm shim preserves constraints it was called
thus `shim mash` shims to `mash*` but `shim mash^0.3` shims to ^0.3
1 parent 41d8d37 commit 2d616c4

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

pkgm.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
semver,
99
utils,
1010
} from "https://deno.land/x/libpkgx@v0.21.0/mod.ts";
11-
import { basename, dirname, join } from "jsr:@std/path@^1";
11+
import { dirname, join } from "jsr:@std/path@^1";
1212
import { ensureDir, existsSync, walk } from "jsr:@std/fs@^1";
1313
import { parseArgs } from "jsr:@std/cli@^1";
1414
const { hydrate } = plumbing;
@@ -211,17 +211,16 @@ async function shim(args: string[], basePath: string) {
211211

212212
const json = (await query_pkgx(pkgx, args))[0];
213213

214-
const projects_we_care_about = [];
215-
for (const pkg of json.pkgs) {
216-
const cmds = await hooks.usePantry().project(pkg.pkg.project).provides();
217-
const set = new Set(cmds.map((x) => basename(x)));
218-
if (!args.some((x) => set.has(x))) continue;
219-
const companions = await hooks.usePantry().project(pkg.pkg.project)
220-
.companions();
221-
projects_we_care_about.push(
222-
pkg.pkg.project,
223-
...companions.map((x) => x.project),
224-
);
214+
const args_pkgs: Record<string, semver.Range> = {};
215+
const projects_we_care_about: string[] = [];
216+
for (const arg of args) {
217+
const pkgs = await hooks.usePantry().find(arg);
218+
if (pkgs.length == 0) throw new Error(`no such pkg: ${arg}`);
219+
if (pkgs.length > 1) throw new Error(`ambiguous pkg: ${arg}`);
220+
args_pkgs[pkgs[0].project] = utils.pkg.parse(arg).constraint;
221+
projects_we_care_about.push(pkgs[0].project);
222+
const companions = await hooks.usePantry().project(pkgs[0]).companions();
223+
projects_we_care_about.push(...companions.map((x) => x.project));
225224
}
226225

227226
for (const pkg of json.pkgs) {
@@ -239,8 +238,10 @@ async function shim(args: string[], basePath: string) {
239238
? "/usr/local/bin/pkgx"
240239
: "/usr/bin/env -S pkgx";
241240

242-
const shim =
243-
`#!${interpreter} --shebang --quiet +${pkg.pkg.project}=${pkg.pkg.version} -- ${name}`;
241+
const range = args_pkgs[pkg.pkg.project];
242+
const arg = `${pkg.pkg.project}${`${range}` == "*" ? "" : `${range}`}`;
243+
244+
const shim = `#!${interpreter} --shebang --quiet +${arg} -- ${name}`;
244245

245246
if (existsSync(join(basePath, "bin", name))) {
246247
await Deno.remove(join(basePath, "bin", name));

0 commit comments

Comments
 (0)