From 860880c69a4ca397f21b2f67190e23ceb312877a Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sun, 7 Aug 2022 17:30:14 -0700 Subject: [PATCH] feat: add isRoot function in addition to isSudo --- packages/sudo-tools/src/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/sudo-tools/src/index.ts b/packages/sudo-tools/src/index.ts index a43e511d..8980f5b7 100644 --- a/packages/sudo-tools/src/index.ts +++ b/packages/sudo-tools/src/index.ts @@ -13,10 +13,15 @@ export function isSudo(): boolean { return isSudoCache } // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unnecessary-condition - isSudoCache = (Boolean(process.env.CI) || process.getuid?.() === 0) && which.sync("sudo", { nothrow: true }) !== null + isSudoCache = (Boolean(process.env.CI) || isRoot()) && which.sync("sudo", { nothrow: true }) !== null return isSudoCache } +/** Detect if the process has root privileges */ +export function isRoot(): boolean { + return process.getuid?.() === 0 +} + /** Prepend `sudo` to the command if sudo is available */ export function prependSudo(command: string) { if (isSudo()) {