( command: string, env: NodeJS.ProcessEnv, )
| 112 | const POWERSHELL_COMMAND_ENV = '__MAKA_RUNTIME_POWERSHELL_COMMAND'; |
| 113 | |
| 114 | function buildPowerShellCommand( |
| 115 | command: string, |
| 116 | env: NodeJS.ProcessEnv, |
| 117 | ): { script: string; env: NodeJS.ProcessEnv } { |
| 118 | // Keep the user command as its own parsed script. Prefixing statements |
| 119 | // directly would invalidate command text that begins with script-level |
| 120 | // syntax such as `using namespace`. Carry it outside argv so wrapping does |
| 121 | // not shrink Windows' command-line budget, then remove the private slot |
| 122 | // before user code or any descendant process can inspect the environment. |
| 123 | const invokeCommand = |
| 124 | `$__makaCommandText = [Environment]::GetEnvironmentVariable('${POWERSHELL_COMMAND_ENV}')\n` + |
| 125 | `[Environment]::SetEnvironmentVariable('${POWERSHELL_COMMAND_ENV}', $null)\n` + |
| 126 | '$__makaCommand = [ScriptBlock]::Create($__makaCommandText)\n' + |
| 127 | '. $__makaCommand'; |
| 128 | const inheritedEnv = Object.fromEntries( |
| 129 | Object.entries(env).filter(([key]) => key.toUpperCase() !== POWERSHELL_COMMAND_ENV), |
| 130 | ); |
| 131 | return { |
| 132 | script: `${POWERSHELL_UTF8_BOOTSTRAP}\n${invokeCommand}`, |
| 133 | env: { |
| 134 | ...inheritedEnv, |
| 135 | [POWERSHELL_COMMAND_ENV]: `${command}\n${POWERSHELL_EXIT_CODE_TAIL}`, |
| 136 | }, |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | export function buildShellSpawnPlan( |
| 141 | shell: ShellPlan, |
no test coverage detected