diff --git a/pkgm.ts b/pkgm.ts index 9d795b6..3bc4f4c 100755 --- a/pkgm.ts +++ b/pkgm.ts @@ -82,7 +82,7 @@ async function install(args: string[]) { }) .spawn(); - let status = await proc.status; + const status = await proc.status; if (!status.success) { Deno.exit(status.code); @@ -107,9 +107,10 @@ 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 needs_sudo = Deno.uid() != 0; - status = await new Deno.Command("/usr/bin/sudo", { - args: [ + if (needs_sudo) { + args = [ "pkgx", "deno^2.1", "run", @@ -120,13 +121,14 @@ async function install(args: string[]) { "sudo-install", pkgx_dir, ...to_install, - ], - env, - clearEnv: true, - }) - .spawn().status; - - Deno.exit(status.code); + ]; + const cmd = "/usr/bin/sudo"; + const status = await new Deno.Command(cmd, { args, env, clearEnv: true }) + .spawn().status; + Deno.exit(status.code); + } else { + await sudo_install(pkgx_dir, to_install); + } } async function sudo_install(pkgx_dir: string, pkg_prefixes: string[]) {