Skip to content

Commit

Permalink
Fix when pkgx is installed by Homebrew on Linux (#23)
Browse files Browse the repository at this point in the history
* Fix when pkgx is installed by Homebrew on Linux

* Search in ~/.linuxbrew too

* Add comment for /usr/local which is also homebrew prefix

* Fix PATH in sudo for real
  • Loading branch information
felipecrs authored Feb 24, 2025
1 parent eed178e commit 7da3fa7
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions pkgm.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
#!/usr/bin/env -S pkgx --quiet deno^2.1 run --ext=ts --allow-sys=uid --allow-run --allow-env=PKGX_DIR,HOMEBREW_PREFIX,HOME --allow-read=/usr/local/pkgs
import { dirname, fromFileUrl, join } from "jsr:@std/path@^1";
import { ensureDir, existsSync } from "jsr:@std/fs@^1";
import { parse as parse_args } from "jsr:@std/[email protected]";
import { parseArgs } from "jsr:@std/cli@^1";
import * as semver from "jsr:@std/semver@^1";

function standardPath() {
const basePath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";
// for pkgm installed via homebrew
const homebrew = `${Deno.env.get("HOMEBREW_PREFIX") || "/opt/homebrew"}/bin`;
if (Deno.build.os === "darwin") {
return `${homebrew}:${basePath}`;
} else {
return basePath;
let path = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";

// for pkgx installed via homebrew
let homebrewPrefix = "";
switch (Deno.build.os) {
case "darwin":
homebrewPrefix = "/opt/homebrew"; // /usr/local is already in the path
break;
case "linux":
homebrewPrefix = `/home/linuxbrew/.linuxbrew:${Deno.env.get("HOME")}/.linuxbrew`;
break;
}
if (homebrewPrefix) {
homebrewPrefix = Deno.env.get("HOMEBREW_PREFIX") ?? homebrewPrefix;
path = `${homebrewPrefix}/bin:${path}`;
}

return path;
}

const parsedArgs = parse_args(Deno.args, {
const parsedArgs = parseArgs(Deno.args, {
alias: {
v: "version",
h: "help",
Expand Down Expand Up @@ -117,7 +127,7 @@ async function install(args: string[]) {
}

const self = fromFileUrl(import.meta.url);
const pkgx_dir = Deno.env.get("PKGX_DIR") || `${Deno.env.get("HOME")}/.pkgx`;
const pkgx_dir = Deno.env.get("PKGX_DIR") ?? `${Deno.env.get("HOME")}/.pkgx`;
const needs_sudo = Deno.uid() != 0;

const runtime_env = expand_runtime_env(json.runtime_env);
Expand All @@ -135,7 +145,16 @@ async function install(args: string[]) {
runtime_env,
...to_install,
];
const cmd = needs_sudo ? "/usr/bin/sudo" : args.shift()!;
let cmd = ""
if (needs_sudo) {
cmd = "/usr/bin/sudo";
args.unshift(
"-E", // we already cleared the env, it's safe
"env", `PATH=${env.PATH}`,
);
} else {
cmd = args.shift()!;
}
status = await new Deno.Command(cmd, { args, env, clearEnv: true })
.spawn().status;
Deno.exit(status.code);
Expand Down

0 comments on commit 7da3fa7

Please sign in to comment.