Skip to content

Commit 7da3fa7

Browse files
authored
Fix when pkgx is installed by Homebrew on Linux (#23)
* 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
1 parent eed178e commit 7da3fa7

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

pkgm.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
#!/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
22
import { dirname, fromFileUrl, join } from "jsr:@std/path@^1";
33
import { ensureDir, existsSync } from "jsr:@std/fs@^1";
4-
import { parse as parse_args } from "jsr:@std/flags@0.224.0";
4+
import { parseArgs } from "jsr:@std/cli@^1";
55
import * as semver from "jsr:@std/semver@^1";
66

77
function standardPath() {
8-
const basePath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";
9-
// for pkgm installed via homebrew
10-
const homebrew = `${Deno.env.get("HOMEBREW_PREFIX") || "/opt/homebrew"}/bin`;
11-
if (Deno.build.os === "darwin") {
12-
return `${homebrew}:${basePath}`;
13-
} else {
14-
return basePath;
8+
let path = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";
9+
10+
// for pkgx installed via homebrew
11+
let homebrewPrefix = "";
12+
switch (Deno.build.os) {
13+
case "darwin":
14+
homebrewPrefix = "/opt/homebrew"; // /usr/local is already in the path
15+
break;
16+
case "linux":
17+
homebrewPrefix = `/home/linuxbrew/.linuxbrew:${Deno.env.get("HOME")}/.linuxbrew`;
18+
break;
1519
}
20+
if (homebrewPrefix) {
21+
homebrewPrefix = Deno.env.get("HOMEBREW_PREFIX") ?? homebrewPrefix;
22+
path = `${homebrewPrefix}/bin:${path}`;
23+
}
24+
25+
return path;
1626
}
1727

18-
const parsedArgs = parse_args(Deno.args, {
28+
const parsedArgs = parseArgs(Deno.args, {
1929
alias: {
2030
v: "version",
2131
h: "help",
@@ -117,7 +127,7 @@ async function install(args: string[]) {
117127
}
118128

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

123133
const runtime_env = expand_runtime_env(json.runtime_env);
@@ -135,7 +145,16 @@ async function install(args: string[]) {
135145
runtime_env,
136146
...to_install,
137147
];
138-
const cmd = needs_sudo ? "/usr/bin/sudo" : args.shift()!;
148+
let cmd = ""
149+
if (needs_sudo) {
150+
cmd = "/usr/bin/sudo";
151+
args.unshift(
152+
"-E", // we already cleared the env, it's safe
153+
"env", `PATH=${env.PATH}`,
154+
);
155+
} else {
156+
cmd = args.shift()!;
157+
}
139158
status = await new Deno.Command(cmd, { args, env, clearEnv: true })
140159
.spawn().status;
141160
Deno.exit(status.code);

0 commit comments

Comments
 (0)