feat: add isRoot function in addition to isSudo

This commit is contained in:
Amin Yahyaabadi 2022-08-07 17:30:14 -07:00
parent bf519e6b3d
commit 860880c69a
1 changed files with 6 additions and 1 deletions

View File

@ -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()) {