Skip to content

Commit 5230647

Browse files
authoredApr 29, 2024··
fix: rewrite powershell scripts to use PSScriptRoot (#7422)
Fixes #7417 Ref nodejs/node#52682
1 parent 762888a commit 5230647

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed
 

‎bin/npm.ps1

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
#!/usr/bin/env pwsh
2-
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
32

4-
$exe=""
5-
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
6-
# Fix case when both the Windows and Linux builds of Node
7-
# are installed in the same directory
8-
$exe=".exe"
3+
$NODE_EXE="$PSScriptRoot/node.exe"
4+
if (-not (Test-Path $NODE_EXE)) {
5+
$NODE_EXE="$PSScriptRoot/node"
96
}
10-
$ret=0
11-
12-
$nodeexe = "node$exe"
13-
$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source
14-
if ($nodebin -eq $null) {
15-
Write-Host "$nodeexe not found."
16-
exit 1
7+
if (-not (Test-Path $NODE_EXE)) {
8+
$NODE_EXE="node"
179
}
18-
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1910

20-
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
21-
$npmprefix=(& $nodeexe $npmprefixjs)
11+
$NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js"
12+
$NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS)
13+
2214
if ($LASTEXITCODE -ne 0) {
2315
Write-Host "Could not determine Node.js install directory"
2416
exit 1
2517
}
26-
$npmprefixclijs="$npmprefix/node_modules/npm/bin/npm-cli.js"
18+
19+
$NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js"
2720

2821
# Support pipeline input
2922
if ($MyInvocation.ExpectingInput) {
30-
$input | & $nodeexe $npmprefixclijs $args
23+
$input | & $NODE_EXE $NPM_CLI_JS $args
3124
} else {
32-
& $nodeexe $npmprefixclijs $args
25+
& $NODE_EXE $NPM_CLI_JS $args
3326
}
34-
$ret=$LASTEXITCODE
35-
exit $ret
27+
28+
exit $LASTEXITCODE

‎bin/npx.ps1

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
#!/usr/bin/env pwsh
2-
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
32

4-
$exe=""
5-
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
6-
# Fix case when both the Windows and Linux builds of Node
7-
# are installed in the same directory
8-
$exe=".exe"
3+
$NODE_EXE="$PSScriptRoot/node.exe"
4+
if (-not (Test-Path $NODE_EXE)) {
5+
$NODE_EXE="$PSScriptRoot/node"
96
}
10-
$ret=0
11-
12-
$nodeexe = "node$exe"
13-
$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source
14-
if ($nodebin -eq $null) {
15-
Write-Host "$nodeexe not found."
16-
exit 1
7+
if (-not (Test-Path $NODE_EXE)) {
8+
$NODE_EXE="node"
179
}
18-
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1910

20-
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
21-
$npmprefix=(& $nodeexe $npmprefixjs)
11+
$NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js"
12+
$NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS)
13+
2214
if ($LASTEXITCODE -ne 0) {
2315
Write-Host "Could not determine Node.js install directory"
2416
exit 1
2517
}
26-
$npmprefixclijs="$npmprefix/node_modules/npm/bin/npx-cli.js"
18+
19+
$NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js"
2720

2821
# Support pipeline input
2922
if ($MyInvocation.ExpectingInput) {
30-
$input | & $nodeexe $npmprefixclijs $args
23+
$input | & $NODE_EXE $NPX_CLI_JS $args
3124
} else {
32-
& $nodeexe $npmprefixclijs $args
25+
& $NODE_EXE $NPX_CLI_JS $args
3326
}
34-
$ret=$LASTEXITCODE
35-
exit $ret
27+
28+
exit $LASTEXITCODE

0 commit comments

Comments
 (0)
Please sign in to comment.