cli
cli copied to clipboard
fix(powershell): use Invoke-Expression to pass args
@lukekarrys Node v22.1.0 was out, and I had to patch "C:\Program Files\nodejs\npm.ps1" again.
My current version:
#!/usr/bin/env pwsh
$NODE_EXE="$PSScriptRoot/node.exe"
if (-not (Test-Path $NODE_EXE)) {
$NODE_EXE="$PSScriptRoot/node"
}
if (-not (Test-Path $NODE_EXE)) {
$NODE_EXE="node"
}
$NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js"
$NPM_CLI_JS="$PSScriptRoot/node_modules/npm/bin/npm-cli.js"
$NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS)
if ($LASTEXITCODE -ne 0) {
Write-Host "Could not determine Node.js install directory"
exit 1
}
$NPM_PREFIX_NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js"
if (Test-Path $NPM_PREFIX_NPM_CLI_JS) {
$NPM_CLI_JS=$NPM_PREFIX_NPM_CLI_JS
}
function Normalize {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[string]$Path
)
$Path = [System.IO.Path]::GetFullPath($Path)
# remove trailing " or ' quotes (if any) and put back " quotes around the path
$Path = $Path -replace '^\s*"\s*(.*?)\s*"\s*$', '$1'
$Path = $Path -replace "^\s*'\s*(.*?)\s*'\s*$", "$1"
return """$Path"""
}
$NPM_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim()
$INVOKE_NPM = "& $(Normalize $NODE_EXE) $(Normalize $NPM_CLI_JS) $NPM_ARGS"
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | Invoke-Expression $INVOKE_NPM
} else {
Invoke-Expression $INVOKE_NPM
}
exit $LASTEXITCODE
Andrew's solution is working well.
Let's merge the last version above and fix this annoying bug.
The logs expired. Luke @lukekarrys, could you please update the PR and trigger CI so we see what's missing?
Thank you 🙏
I won't be able to push this over the finish line. It's probably best if a new PR is opened with the proposed solution.
Andrew's solution is working well.
@mbtools , thanks for vouching! I currently re-apply this patch semi-automatically everytime I upgrade node.js on Windows: https://github.com/npm/cli/issues/7375#issuecomment-2437224225
I'm sorry I don't have capacity right now to re-opon this PR, but I'd be grateful if anyone could pick it up.
@noseratio I used your newer patch to open https://github.com/npm/cli/pull/8267, and did the same for npx.ps1 Thanks for the patch!
EDIT: I changed $MyInvocation.Statement to $MyInvocation.Line since Statement doesn't work in Windows PowerShell
There are differences between Windows PowerShell, PowerShell before 7.3 and PowerShell >= 7.3. We need to be clear what works and what doesn't (see my comment in the PR)