Files
orca/src/main/cli/cli-privileged-processes.ts
T
2026-09-25 23:13:31 -07:00

53 lines
1.8 KiB
TypeScript

import { runProcess } from '../../shared/child-process/run-process'
import { quotePowerShellLiteral } from '../../shared/powershell-native-argument'
import { WINDOWS_PATH_WRITE_TIMEOUT_MS } from './cli-install-constants'
export async function runMacPrivilegedCommand(command: string): Promise<void> {
const result = await runProcess({
program: 'osascript',
args: ['-e', `do shell script ${quoteAppleScript(command)} with administrator privileges`],
// Why: the OS authorization prompt is user-paced and previously had no deadline.
timeoutMs: null
})
if (result.code !== 0) {
throw processFailure('osascript', result)
}
}
function quoteAppleScript(value: string): string {
return `"${value.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`
}
export async function writeWindowsUserPath(value: string): Promise<void> {
await runWindowsPathCommand([
'-NoProfile',
'-Command',
`[Environment]::SetEnvironmentVariable('Path', ${quotePowerShellLiteral(value)}, 'User')`
])
}
export async function runWindowsPathCommand(args: string[]): Promise<string> {
const result = await runProcess({
program: 'powershell',
args,
timeoutMs: WINDOWS_PATH_WRITE_TIMEOUT_MS
})
if (result.timedOut) {
throw new Error(`Windows PATH command timed out after ${WINDOWS_PATH_WRITE_TIMEOUT_MS}ms.`)
}
if (result.code !== 0) {
throw processFailure('powershell', result)
}
return result.stdout
}
function processFailure(
program: string,
result: { code: number | null; stderr: string; stdout: string }
): Error {
const detail = result.stderr || result.stdout
const error = new Error(detail || `${program} exited with code ${result.code ?? 'unknown'}`)
Object.assign(error, { code: result.code, stderr: result.stderr })
return error
}