Skip to content

Commit 83ebf42

Browse files
author
L. Yeung
authored
fix: do not use dynamic module in post-script for PowerShell (#254)
1 parent 10bc3ce commit 83ebf42

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

lib/auto.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,19 @@ function enableAutoSwitch(enable) {
112112
require('./postScript').generate(null, {
113113
'.PS1': [
114114
// Patch the function that is invoked every time PowerShell shows a prompt.
115-
// Export the function from the script using a dynamic module; this
116-
// does NOT require the script to be sourced.
117-
'if (-not $env:NVS_ORIGINAL_PROMPT) { ',
118-
' $env:NVS_ORIGINAL_PROMPT = $(Get-Content function:\\prompt)',
115+
// This does NOT require the script to be sourced.
116+
'if (-not $global:NVS_ORIGINAL_PROMPT) {',
117+
' $global:NVS_ORIGINAL_PROMPT = $Function:prompt',
118+
'}',
119+
'function global:prompt {',
120+
' # We have to do this so a prompt customization tool (like Oh My Posh or Starship) can get',
121+
' # the correct last command execution status and native command return code.',
122+
' $global:NVS_ORIGINAL_LASTEXECUTIONSTATUS = $?',
123+
' $originalExitCode = $global:LASTEXITCODE',
124+
' . "' + psScriptFile + '" "prompt"',
125+
' $global:LASTEXITCODE = $originalExitCode',
126+
' $global:NVS_ORIGINAL_PROMPT.Invoke()',
119127
'}',
120-
'New-Module -Script {',
121-
' function prompt { . "' + psScriptFile + '" "prompt" }',
122-
' Export-ModuleMember -Function prompt',
123-
'} > $null',
124128
],
125129
'.SH': [
126130
'function cd () { builtin cd "$@" && nvs cd; }',
@@ -131,11 +135,9 @@ function enableAutoSwitch(enable) {
131135
} else {
132136
require('./postScript').generate(null, {
133137
'.PS1': [
134-
'if ($env:NVS_ORIGINAL_PROMPT) { ',
135-
' New-Module -Script {',
136-
' function prompt { Invoke-Expression $env:NVS_ORIGINAL_PROMPT }',
137-
' Export-ModuleMember -Function prompt',
138-
' } > $null',
138+
'if ($global:NVS_ORIGINAL_PROMPT) {',
139+
' $Function:prompt = $global:NVS_ORIGINAL_PROMPT',
140+
' Remove-Variable -Name @("NVS_ORIGINAL_PROMPT", "NVS_ORIGINAL_LASTEXECUTIONSTATUS") -Scope global',
139141
'}',
140142
],
141143
'.SH': [

nvs.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ if ($args -eq "bootstrap") {
8585
exit 0
8686
}
8787
elseif ($args -eq "prompt") {
88-
# This script was invoked as a PS prompt function that enables auto-switching.
89-
Invoke-Expression $env:NVS_ORIGINAL_PROMPT
88+
# This script was invoked in a PS prompt function that enables auto-switching.
9089

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

0 commit comments

Comments
 (0)