2023-04-22 17:19:33 +08:00
|
|
|
import * as execa from "execa"
|
2022-08-08 07:59:24 +08:00
|
|
|
import which from "which"
|
|
|
|
|
|
|
|
/** The cached powershell path */
|
|
|
|
let powershell: string | undefined
|
|
|
|
|
|
|
|
/**
|
2022-08-08 08:34:40 +08:00
|
|
|
* Asynchronously execute a powershell command.
|
2022-08-08 07:59:24 +08:00
|
|
|
*
|
|
|
|
* @param command The powershell command to execute
|
2022-08-08 09:18:26 +08:00
|
|
|
* @param startupFlags The optional startup flags to be passed to powershell. Defaults to `["-NoProfile", "-NoLogo",
|
|
|
|
* "-NonInteractive"]`. This means that the Powershell profile is not sourced first.
|
|
|
|
* @param execOptions The options passed to `execa`. Defaults to `{ stdio: "inherit" }`
|
|
|
|
* @returns A promise to the execution result
|
2022-08-08 07:59:24 +08:00
|
|
|
* @note It prefers `pwsh` over `powershell`
|
|
|
|
*/
|
|
|
|
export function execPowershell(
|
|
|
|
command: string,
|
|
|
|
startupFlags: string[] = ["-NoProfile", "-NoLogo", "-NonInteractive"],
|
|
|
|
execOptions: execa.Options = { stdio: "inherit" }
|
2022-08-08 09:18:26 +08:00
|
|
|
): execa.ExecaChildProcess<string> {
|
2023-04-22 17:19:33 +08:00
|
|
|
return execa.execa(getPowerShell(), [...startupFlags, "-c", command], execOptions)
|
2022-08-08 07:59:24 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 08:36:27 +08:00
|
|
|
/**
|
|
|
|
* Execute a powershell command.
|
|
|
|
*
|
|
|
|
* @param command The powershell command to execute
|
2022-08-08 09:18:26 +08:00
|
|
|
* @param startupFlags The optional startup flags to be passed to powershell. Defaults to `["-NoProfile", "-NoLogo",
|
|
|
|
* "-NonInteractive"]`. This means that the Powershell profile is not sourced first.
|
|
|
|
* @param execOptions The options passed to `execa`. Defaults to `{ stdio: "inherit" }`
|
|
|
|
* @returns The execution result
|
2022-08-08 08:36:27 +08:00
|
|
|
* @note It prefers `pwsh` over `powershell`
|
|
|
|
*/
|
|
|
|
export function execPowershellSync(
|
|
|
|
command: string,
|
|
|
|
startupFlags: string[] = ["-NoProfile", "-NoLogo", "-NonInteractive"],
|
|
|
|
execOptions: execa.SyncOptions = { stdio: "inherit" }
|
2022-08-08 09:18:26 +08:00
|
|
|
): execa.ExecaSyncReturnValue<string> {
|
2023-04-22 17:19:33 +08:00
|
|
|
return execa.execaSync(getPowerShell(), [...startupFlags, "-c", command], execOptions)
|
2022-08-08 08:36:27 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 07:59:24 +08:00
|
|
|
/**
|
|
|
|
* Get the path to the powershell executable.
|
|
|
|
*
|
|
|
|
* @note It prefers `pwsh` over `powershell`
|
|
|
|
* @note It caches the path for the subsequent calls to this function
|
|
|
|
*/
|
|
|
|
export function getPowerShell() {
|
|
|
|
if (powershell === undefined) {
|
|
|
|
const maybePwsh = which.sync("pwsh", { nothrow: true })
|
|
|
|
if (maybePwsh !== null) {
|
|
|
|
powershell = maybePwsh
|
|
|
|
}
|
|
|
|
const maybePowerShell = which.sync("powershell", { nothrow: true })
|
|
|
|
if (maybePowerShell !== null) {
|
|
|
|
powershell = maybePowerShell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (powershell === undefined) {
|
|
|
|
throw new Error("Could not find powershell")
|
|
|
|
}
|
|
|
|
return powershell
|
|
|
|
}
|