From 9cd6acef6147e49279663212a46913458e773053 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Thu, 25 Apr 2024 15:47:29 -0700 Subject: [PATCH 1/3] fix: rewrite powershell scripts to use PSScriptRoot Fixes #7417 Ref https://github.com/nodejs/node/issues/52682 --- bin/npm.ps1 | 40 +++++++++++++++------------------------- bin/npx.ps1 | 40 +++++++++++++++------------------------- 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 399e33360e853..4d507edbe6143 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -1,35 +1,25 @@ #!/usr/bin/env pwsh -$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent -$exe="" -if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { - # Fix case when both the Windows and Linux builds of Node - # are installed in the same directory - $exe=".exe" -} -$ret=0 +$RET=0 -$nodeexe = "node$exe" -$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source -if ($nodebin -eq $null) { - Write-Host "$nodeexe not found." - exit 1 +$NODE_EXE="$PSScriptRoot/node.exe" +if (-not (Test-Path $NODE_EXE)) { + $NODE_EXE="$PSScriptRoot/node" } -$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path - -$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js" -$npmprefix=(& $nodeexe $npmprefixjs) -if ($LASTEXITCODE -ne 0) { - Write-Host "Could not determine Node.js install directory" - exit 1 +if (-not (Test-Path $NODE_EXE)) { + $NODE_EXE="node" } -$npmprefixclijs="$npmprefix/node_modules/npm/bin/npm-cli.js" + +$NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js" +$NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS) +$NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js" # Support pipeline input if ($MyInvocation.ExpectingInput) { - $input | & $nodeexe $npmprefixclijs $args + $input | & $NODE_EXE $NPM_CLI_JS $args } else { - & $nodeexe $npmprefixclijs $args + & $NODE_EXE $NPM_CLI_JS $args } -$ret=$LASTEXITCODE -exit $ret + +$RET=$LASTEXITCODE +exit $RET diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 1d59fc52083d7..9065c1cabbb24 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -1,35 +1,25 @@ #!/usr/bin/env pwsh -$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent -$exe="" -if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { - # Fix case when both the Windows and Linux builds of Node - # are installed in the same directory - $exe=".exe" -} -$ret=0 +$RET=0 -$nodeexe = "node$exe" -$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source -if ($nodebin -eq $null) { - Write-Host "$nodeexe not found." - exit 1 +$NODE_EXE="$PSScriptRoot/node.exe" +if (-not (Test-Path $NODE_EXE)) { + $NODE_EXE="$PSScriptRoot/node" } -$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path - -$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js" -$npmprefix=(& $nodeexe $npmprefixjs) -if ($LASTEXITCODE -ne 0) { - Write-Host "Could not determine Node.js install directory" - exit 1 +if (-not (Test-Path $NODE_EXE)) { + $NODE_EXE="node" } -$npmprefixclijs="$npmprefix/node_modules/npm/bin/npx-cli.js" + +$NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js" +$NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS) +$NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js" # Support pipeline input if ($MyInvocation.ExpectingInput) { - $input | & $nodeexe $npmprefixclijs $args + $input | & $NODE_EXE $NPX_CLI_JS $args } else { - & $nodeexe $npmprefixclijs $args + & $NODE_EXE $NPX_CLI_JS $args } -$ret=$LASTEXITCODE -exit $ret + +$RET=$LASTEXITCODE +exit $RET From 9e46941f96c257e902a77ee0f23201e1de3a5600 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Thu, 25 Apr 2024 15:53:37 -0700 Subject: [PATCH 2/3] fixup: bring back error when node cant be found and cleanup exit code --- bin/npm.ps1 | 17 ++++++++++------- bin/npx.ps1 | 11 +++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 4d507edbe6143..d47661dc5cf70 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -1,7 +1,5 @@ #!/usr/bin/env pwsh -$RET=0 - $NODE_EXE="$PSScriptRoot/node.exe" if (-not (Test-Path $NODE_EXE)) { $NODE_EXE="$PSScriptRoot/node" @@ -12,14 +10,19 @@ if (-not (Test-Path $NODE_EXE)) { $NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js" $NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS) -$NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js" + +if ($LASTEXITCODE -ne 0) { + Write-Host "Could not determine Node.js install directory" + exit 1 +} + +$NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js" # Support pipeline input if ($MyInvocation.ExpectingInput) { - $input | & $NODE_EXE $NPM_CLI_JS $args + $input | & $NODE_EXE $NPX_CLI_JS $args } else { - & $NODE_EXE $NPM_CLI_JS $args + & $NODE_EXE $NPX_CLI_JS $args } -$RET=$LASTEXITCODE -exit $RET +exit $LASTEXITCODE diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 9065c1cabbb24..01b851a82e8bb 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -1,7 +1,5 @@ #!/usr/bin/env pwsh -$RET=0 - $NODE_EXE="$PSScriptRoot/node.exe" if (-not (Test-Path $NODE_EXE)) { $NODE_EXE="$PSScriptRoot/node" @@ -12,6 +10,12 @@ if (-not (Test-Path $NODE_EXE)) { $NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js" $NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS) + +if ($LASTEXITCODE -ne 0) { + Write-Host "Could not determine Node.js install directory" + exit 1 +} + $NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js" # Support pipeline input @@ -21,5 +25,4 @@ if ($MyInvocation.ExpectingInput) { & $NODE_EXE $NPX_CLI_JS $args } -$RET=$LASTEXITCODE -exit $RET +exit $LASTEXITCODE From ab8a779776c985e951562c81a7d7fae54708d92d Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Thu, 25 Apr 2024 16:03:16 -0700 Subject: [PATCH 3/3] fixup: fix name of script variable --- bin/npm.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index d47661dc5cf70..797004fd3dc42 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -16,13 +16,13 @@ if ($LASTEXITCODE -ne 0) { exit 1 } -$NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js" +$NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js" # Support pipeline input if ($MyInvocation.ExpectingInput) { - $input | & $NODE_EXE $NPX_CLI_JS $args + $input | & $NODE_EXE $NPM_CLI_JS $args } else { - & $NODE_EXE $NPX_CLI_JS $args + & $NODE_EXE $NPM_CLI_JS $args } exit $LASTEXITCODE