Skip to content

Commit 43e2381

Browse files
authored
Merge pull request #61 from pkgxdev/shim-preserves-constraints
`pkgm shim` preserves constraints it was called
2 parents 41d8d37 + 91cf106 commit 43e2381

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ jobs:
6363
- run: ~/.local/bin/semverator validate 1.0.0
6464

6565
# tests shims do not shim deps
66-
- run: ./pkgm.ts shim node
66+
- run: ./pkgm.ts shim node@20
6767
- run: test ! -d ~/.local/bin/openssl
68+
- run: if [[ $(~/.local/bin/node --version) != v20* ]]; then false; fi
6869

6970
- run: ./pkgm.ts i hyperfine@1.18
7071
- run: ./pkgm.ts outdated | grep hyperfine

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)