Skip to content

Commit 9633e40

Browse files
authored
Merge pull request #40 from pkgxdev/mkpkgxable
fix pkging to pkgx
2 parents 0252b80 + 0e0c797 commit 9633e40

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

pkgm.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async function install(args: string[], basePath: string) {
122122

123123
const self = fromFileUrl(import.meta.url);
124124
const pkgx_dir = Deno.env.get("PKGX_DIR") || `${Deno.env.get("HOME")}/.pkgx`;
125-
const needs_sudo = Deno.uid() != 0 && basePath === "/usr/local";
125+
const needs_sudo = basePath === "/usr/local" && !writable("/usr/local");
126126

127127
const runtime_env = expand_runtime_env(json, basePath);
128128

@@ -493,7 +493,8 @@ async function uninstall(arg: string) {
493493
Deno.exit(1);
494494
}
495495

496-
const needs_sudo = files.some((p) => p.string.startsWith("/usr/local"));
496+
const needs_sudo = files.some((p) => p.string.startsWith("/usr/local")) &&
497+
!writable("/usr/local");
497498
if (needs_sudo) {
498499
{
499500
const { success, code } = await new Deno.Command("/usr/bin/sudo", {
@@ -546,3 +547,14 @@ async function uninstall(arg: string) {
546547
}
547548
}
548549
}
550+
551+
function writable(path: string) {
552+
try {
553+
//FIXME this is pretty gross
554+
Deno.mkdirSync(join(path, ".writable_test"), { recursive: true });
555+
Deno.remove(join(path, ".writable_test"));
556+
return true;
557+
} catch {
558+
return false;
559+
}
560+
}

0 commit comments

Comments
 (0)