Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not use dynamic module in post-script for PowerShell #254

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions lib/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,19 @@ function enableAutoSwitch(enable) {
require('./postScript').generate(null, {
'.PS1': [
// Patch the function that is invoked every time PowerShell shows a prompt.
// Export the function from the script using a dynamic module; this
// does NOT require the script to be sourced.
'if (-not $env:NVS_ORIGINAL_PROMPT) { ',
' $env:NVS_ORIGINAL_PROMPT = $(Get-Content function:\\prompt)',
// This does NOT require the script to be sourced.
'if (-not $global:NVS_ORIGINAL_PROMPT) {',
' $global:NVS_ORIGINAL_PROMPT = $Function:prompt',
'}',
'function global:prompt {',
' # We have to do this so a prompt customization tool (like Oh My Posh or Starship) can get',
' # the correct last command execution status and native command return code.',
' $global:NVS_ORIGINAL_LASTEXECUTIONSTATUS = $?',
' $originalExitCode = $global:LASTEXITCODE',
' . "' + psScriptFile + '" "prompt"',
' $global:LASTEXITCODE = $originalExitCode',
' $global:NVS_ORIGINAL_PROMPT.Invoke()',
'}',
'New-Module -Script {',
' function prompt { . "' + psScriptFile + '" "prompt" }',
' Export-ModuleMember -Function prompt',
'} > $null',
],
'.SH': [
'function cd () { builtin cd "$@" && nvs cd; }',
Expand All @@ -131,11 +135,9 @@ function enableAutoSwitch(enable) {
} else {
require('./postScript').generate(null, {
'.PS1': [
'if ($env:NVS_ORIGINAL_PROMPT) { ',
' New-Module -Script {',
' function prompt { Invoke-Expression $env:NVS_ORIGINAL_PROMPT }',
' Export-ModuleMember -Function prompt',
' } > $null',
'if ($global:NVS_ORIGINAL_PROMPT) {',
' $Function:prompt = $global:NVS_ORIGINAL_PROMPT',
' Remove-Variable -Name @("NVS_ORIGINAL_PROMPT", "NVS_ORIGINAL_LASTEXECUTIONSTATUS") -Scope global',
'}',
],
'.SH': [
Expand Down
5 changes: 2 additions & 3 deletions nvs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ if ($args -eq "bootstrap") {
exit 0
}
elseif ($args -eq "prompt") {
# This script was invoked as a PS prompt function that enables auto-switching.
Invoke-Expression $env:NVS_ORIGINAL_PROMPT
# This script was invoked in a PS prompt function that enables auto-switching.

# Find the nearest .node-version file in current or parent directories
for ($parentDir = $pwd.Path; $parentDir; $parentDir = Split-Path $parentDir) {
Expand All @@ -109,7 +108,7 @@ elseif ($args -eq "prompt") {
while (($b = $proc.StandardOutput.Read()) -ne -1) {
Write-Host -NoNewline ([char]$b)
}
$proc.WaitForExit
$proc.WaitForExit()
$exitCode = $proc.ExitCode
}
else {
Expand Down