feat: add async execRoot

This commit is contained in:
Amin Yahyaabadi 2022-08-07 17:34:40 -07:00
parent e495d4d0d5
commit 897c5f67c8
2 changed files with 18 additions and 1 deletions

View File

@ -5,7 +5,7 @@ import which from "which"
let powershell: string | undefined let powershell: string | undefined
/** /**
* Execute a powershell command. * Asynchronously execute a powershell command.
* *
* @param command The powershell command to execute * @param command The powershell command to execute
* @param startupFlags The optional startup flags to be passed to powershell. * @param startupFlags The optional startup flags to be passed to powershell.

View File

@ -39,3 +39,20 @@ export function execRootSync(
return execa.sync(program, args, execOptions) return execa.sync(program, args, execOptions)
} }
} }
/**
* Asynchronously execute a command as root if sudo is available. Otherwise executes the command normally without sudo.
*
* @param program The program to spawn
* @param args The command arguments
* @param execOptions The options passed to `execa`.
*
* Defaults to `{ stdio: "inherit" }`
*/
export function execRoot(program: string, args: string[] = [], execOptions: execa.Options = { stdio: "inherit" }) {
if (isSudo()) {
return execa.command(`sudo ${[program, ...args].map((arg) => `'${arg}'`).join(" ")}`, execOptions)
} else {
return execa(program, args, execOptions)
}
}