From b480f56d4dbd549d40368c7abc848a014e8944e1 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 22 May 2025 08:56:12 -0400 Subject: [PATCH 1/7] fix(powershell): multiple Invoke-Expression fixes --- bin/npm.ps1 | 51 ++++++++++++++++++++++++++++++++++++--------------- bin/npx.ps1 | 51 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 77bc9a5777c80..c3c796c7b4b7e 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -22,34 +22,55 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { $NPM_CLI_JS=$NPM_PREFIX_NPM_CLI_JS } -if ($MyInvocation.Line) { # used "-Command" argument +if ($MyInvocation.ExpectingInput) { # takes pipeline input + $input | & $NODE_EXE $NPM_CLI_JS $args +} elseif (-not $MyInvocation.Line) { # used "-File" argument + & $NODE_EXE $NPM_CLI_JS $args +} else { # used "-Command" argument if ($MyInvocation.Statement) { - $NPM_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim() + $NPM_OG_COMMAND = $MyInvocation.Statement } else { $NPM_OG_COMMAND = ( [System.Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [System.Reflection.BindingFlags] 'Instance, NonPublic') ).GetValue($MyInvocation).Text - $NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") - # Support pipeline input - if ($MyInvocation.ExpectingInput) { - $input = (@($input) -join "`n").Replace("``", "````") + $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPM_OG_COMMAND, [ref]$null, [ref]$null) + $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) - Invoke-Expression "Write-Output `"$input`" | & `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" - } else { - Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" - } -} else { # used "-File" argument - # Support pipeline input - if ($MyInvocation.ExpectingInput) { - $input | & $NODE_EXE $NPM_CLI_JS $args + $prevEndOffset = 0 + $i = 0 + $numberOfRedirects = @($redirections).Length + + if ($numberOfRedirects -gt 0) { + $NPM_NO_REDIRECTS_COMMAND = "" + + foreach ($redirection in $redirections) { + $parentExtentText = $redirection.Parent.Extent.Text + $startOffset = $redirection.Extent.StartOffset + $endOffset = $redirection.Extent.EndOffset + + if ($i -lt $numberOfRedirects-1) { + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + } else { + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + } + + $NPM_NO_REDIRECTS_COMMAND += $changed + + $prevEndOffset = $endOffset + $i++ + } + + $NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } else { - & $NODE_EXE $NPM_CLI_JS $args + $NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } + + Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" } exit $LASTEXITCODE diff --git a/bin/npx.ps1 b/bin/npx.ps1 index e89536bf3542a..4be74792ecc6f 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -22,34 +22,55 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { $NPX_CLI_JS=$NPM_PREFIX_NPX_CLI_JS } -if ($MyInvocation.Line) { # used "-Command" argument +if ($MyInvocation.ExpectingInput) { # takes pipeline input + $input | & $NODE_EXE $NPX_CLI_JS $args +} elseif (-not $MyInvocation.Line) { # used "-File" argument + & $NODE_EXE $NPX_CLI_JS $args +} else { # used "-Command" argument if ($MyInvocation.Statement) { - $NPX_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim() + $NPX_OG_COMMAND = $MyInvocation.Statement } else { $NPX_OG_COMMAND = ( [System.Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [System.Reflection.BindingFlags] 'Instance, NonPublic') ).GetValue($MyInvocation).Text - $NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") - # Support pipeline input - if ($MyInvocation.ExpectingInput) { - $input = (@($input) -join "`n").Replace("``", "````") + $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPX_OG_COMMAND, [ref]$null, [ref]$null) + $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) - Invoke-Expression "Write-Output `"$input`" | & `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" - } else { - Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" - } -} else { # used "-File" argument - # Support pipeline input - if ($MyInvocation.ExpectingInput) { - $input | & $NODE_EXE $NPX_CLI_JS $args + $prevEndOffset = 0 + $i = 0 + $numberOfRedirects = @($redirections).Length + + if ($numberOfRedirects -gt 0) { + $NPX_NO_REDIRECTS_COMMAND = "" + + foreach ($redirection in $redirections) { + $parentExtentText = $redirection.Parent.Extent.Text + $startOffset = $redirection.Extent.StartOffset + $endOffset = $redirection.Extent.EndOffset + + if ($i -lt $numberOfRedirects-1) { + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + } else { + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + } + + $NPX_NO_REDIRECTS_COMMAND += $changed + + $prevEndOffset = $endOffset + $i++ + } + + $NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } else { - & $NODE_EXE $NPX_CLI_JS $args + $NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } + + Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" } exit $LASTEXITCODE From 6775b8000e36abfbb87146c2d1ed3077cac569e4 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 22 May 2025 09:32:54 -0400 Subject: [PATCH 2/7] formatting/organization --- bin/npm.ps1 | 6 ++---- bin/npx.ps1 | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index c3c796c7b4b7e..323fc627c8063 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -40,13 +40,12 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPM_OG_COMMAND, [ref]$null, [ref]$null) $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) - - $prevEndOffset = 0 - $i = 0 $numberOfRedirects = @($redirections).Length if ($numberOfRedirects -gt 0) { $NPM_NO_REDIRECTS_COMMAND = "" + $prevEndOffset = 0 + $i = 0 foreach ($redirection in $redirections) { $parentExtentText = $redirection.Parent.Extent.Text @@ -60,7 +59,6 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input } $NPM_NO_REDIRECTS_COMMAND += $changed - $prevEndOffset = $endOffset $i++ } diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 4be74792ecc6f..06539d4a36582 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -40,13 +40,12 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPX_OG_COMMAND, [ref]$null, [ref]$null) $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) - - $prevEndOffset = 0 - $i = 0 $numberOfRedirects = @($redirections).Length if ($numberOfRedirects -gt 0) { $NPX_NO_REDIRECTS_COMMAND = "" + $prevEndOffset = 0 + $i = 0 foreach ($redirection in $redirections) { $parentExtentText = $redirection.Parent.Extent.Text @@ -60,7 +59,6 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input } $NPX_NO_REDIRECTS_COMMAND += $changed - $prevEndOffset = $endOffset $i++ } From 0fb5ed7d3af478a36979f7dbfda08693bc38c252 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 22 May 2025 12:11:15 -0400 Subject: [PATCH 3/7] More fixes --- bin/npm.ps1 | 17 +++++++++++++---- bin/npx.ps1 | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 323fc627c8063..924e37850bda8 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -39,7 +39,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPM_OG_COMMAND, [ref]$null, [ref]$null) - $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) + $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) | Sort-Object { $_.Extent.StartOffset } $numberOfRedirects = @($redirections).Length if ($numberOfRedirects -gt 0) { @@ -52,10 +52,14 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $startOffset = $redirection.Extent.StartOffset $endOffset = $redirection.Extent.EndOffset - if ($i -lt $numberOfRedirects-1) { - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + if ($startOffset - $prevEndOffset -lt 0) { + $changed = $parentExtentText.Substring($prevEndOffset, 0) } else { - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + } + + if ($i -eq $numberOfRedirects-1) { + $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) } $NPM_NO_REDIRECTS_COMMAND += $changed @@ -63,6 +67,11 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $i++ } + $NPM_NO_REDIRECTS_COMMAND = $NPM_NO_REDIRECTS_COMMAND.Trim() + if ($NPM_NO_REDIRECTS_COMMAND.EndsWith("``")) { + $NPM_NO_REDIRECTS_COMMAND = $NPM_NO_REDIRECTS_COMMAND.Substring(0, $NPM_NO_REDIRECTS_COMMAND.Length - 1) + ";" + } + $NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } else { $NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 06539d4a36582..9e16408b4ab96 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -39,7 +39,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPX_OG_COMMAND, [ref]$null, [ref]$null) - $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) + $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) | Sort-Object { $_.Extent.StartOffset } $numberOfRedirects = @($redirections).Length if ($numberOfRedirects -gt 0) { @@ -52,10 +52,14 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $startOffset = $redirection.Extent.StartOffset $endOffset = $redirection.Extent.EndOffset - if ($i -lt $numberOfRedirects-1) { - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + if ($startOffset - $prevEndOffset -lt 0) { + $changed = $parentExtentText.Substring($prevEndOffset, 0) } else { - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) + } + + if ($i -eq $numberOfRedirects-1) { + $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) } $NPX_NO_REDIRECTS_COMMAND += $changed @@ -63,6 +67,11 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $i++ } + $NPX_NO_REDIRECTS_COMMAND = $NPX_NO_REDIRECTS_COMMAND.Trim() + if ($NPX_NO_REDIRECTS_COMMAND.EndsWith("``")) { + $NPX_NO_REDIRECTS_COMMAND = $NPX_NO_REDIRECTS_COMMAND.Substring(0, $NPX_NO_REDIRECTS_COMMAND.Length - 1) + ";" + } + $NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } else { $NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() From 03cf708f565baeb6c8a052d6a5ee158ae5b0ab48 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 22 May 2025 12:58:14 -0400 Subject: [PATCH 4/7] simplify logic --- bin/npm.ps1 | 7 +------ bin/npx.ps1 | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 924e37850bda8..53799a1da2658 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -52,12 +52,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $startOffset = $redirection.Extent.StartOffset $endOffset = $redirection.Extent.EndOffset - if ($startOffset - $prevEndOffset -lt 0) { - $changed = $parentExtentText.Substring($prevEndOffset, 0) - } else { - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) - } - + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) if ($i -eq $numberOfRedirects-1) { $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) } diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 9e16408b4ab96..927a46ef731f7 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -52,12 +52,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $startOffset = $redirection.Extent.StartOffset $endOffset = $redirection.Extent.EndOffset - if ($startOffset - $prevEndOffset -lt 0) { - $changed = $parentExtentText.Substring($prevEndOffset, 0) - } else { - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) - } - + $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) if ($i -eq $numberOfRedirects-1) { $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) } From cdf7174021a1f6f4091e57f6c43abaf84aa54d4a Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Thu, 22 May 2025 14:55:59 -0400 Subject: [PATCH 5/7] Last thing --- bin/npm.ps1 | 12 ++++++------ bin/npx.ps1 | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 53799a1da2658..ca53f3fbfb3b0 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -54,7 +54,12 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) if ($i -eq $numberOfRedirects-1) { - $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + if ($endOffset -eq $parentExtentText.Length) { + ### make valid powershell syntax when redirect on last line is removed + $changed += ";" + } else { + $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + } } $NPM_NO_REDIRECTS_COMMAND += $changed @@ -62,11 +67,6 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $i++ } - $NPM_NO_REDIRECTS_COMMAND = $NPM_NO_REDIRECTS_COMMAND.Trim() - if ($NPM_NO_REDIRECTS_COMMAND.EndsWith("``")) { - $NPM_NO_REDIRECTS_COMMAND = $NPM_NO_REDIRECTS_COMMAND.Substring(0, $NPM_NO_REDIRECTS_COMMAND.Length - 1) + ";" - } - $NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } else { $NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 927a46ef731f7..081a6d2f87143 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -54,7 +54,12 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) if ($i -eq $numberOfRedirects-1) { - $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + if ($endOffset -eq $parentExtentText.Length) { + ### make valid powershell syntax when redirect on last line is removed + $changed += ";" + } else { + $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) + } } $NPX_NO_REDIRECTS_COMMAND += $changed @@ -62,11 +67,6 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $i++ } - $NPX_NO_REDIRECTS_COMMAND = $NPX_NO_REDIRECTS_COMMAND.Trim() - if ($NPX_NO_REDIRECTS_COMMAND.EndsWith("``")) { - $NPX_NO_REDIRECTS_COMMAND = $NPX_NO_REDIRECTS_COMMAND.Substring(0, $NPX_NO_REDIRECTS_COMMAND.Length - 1) + ";" - } - $NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } else { $NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() From be3cceb24b5ee2e58f57d04c5b634112cef55f82 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Fri, 23 May 2025 10:02:23 -0400 Subject: [PATCH 6/7] reduce new code added in half --- bin/npm.ps1 | 38 ++++---------------------------------- bin/npx.ps1 | 38 ++++---------------------------------- 2 files changed, 8 insertions(+), 68 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index ca53f3fbfb3b0..b5ce953b71552 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -31,46 +31,16 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NPM_OG_COMMAND = $MyInvocation.Statement } else { $NPM_OG_COMMAND = ( - [System.Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [System.Reflection.BindingFlags] 'Instance, NonPublic') + [Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic') ).GetValue($MyInvocation).Text } $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") - $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPM_OG_COMMAND, [ref]$null, [ref]$null) - $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) | Sort-Object { $_.Extent.StartOffset } - $numberOfRedirects = @($redirections).Length - - if ($numberOfRedirects -gt 0) { - $NPM_NO_REDIRECTS_COMMAND = "" - $prevEndOffset = 0 - $i = 0 - - foreach ($redirection in $redirections) { - $parentExtentText = $redirection.Parent.Extent.Text - $startOffset = $redirection.Extent.StartOffset - $endOffset = $redirection.Extent.EndOffset - - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) - if ($i -eq $numberOfRedirects-1) { - if ($endOffset -eq $parentExtentText.Length) { - ### make valid powershell syntax when redirect on last line is removed - $changed += ";" - } else { - $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) - } - } - - $NPM_NO_REDIRECTS_COMMAND += $changed - $prevEndOffset = $endOffset - $i++ - } - - $NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() - } else { - $NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() - } + $NPM_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPM_OG_COMMAND, [ref] $null, [ref] $null). + EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' ' + $NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" } diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 081a6d2f87143..59fe3c1abfbf7 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -31,46 +31,16 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NPX_OG_COMMAND = $MyInvocation.Statement } else { $NPX_OG_COMMAND = ( - [System.Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [System.Reflection.BindingFlags] 'Instance, NonPublic') + [Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic') ).GetValue($MyInvocation).Text } $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") - $ast = [System.Management.Automation.Language.Parser]::ParseInput($NPX_OG_COMMAND, [ref]$null, [ref]$null) - $redirections = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.FileRedirectionAst]}, $true) | Sort-Object { $_.Extent.StartOffset } - $numberOfRedirects = @($redirections).Length - - if ($numberOfRedirects -gt 0) { - $NPX_NO_REDIRECTS_COMMAND = "" - $prevEndOffset = 0 - $i = 0 - - foreach ($redirection in $redirections) { - $parentExtentText = $redirection.Parent.Extent.Text - $startOffset = $redirection.Extent.StartOffset - $endOffset = $redirection.Extent.EndOffset - - $changed = $parentExtentText.Substring($prevEndOffset, $startOffset - $prevEndOffset) - if ($i -eq $numberOfRedirects-1) { - if ($endOffset -eq $parentExtentText.Length) { - ### make valid powershell syntax when redirect on last line is removed - $changed += ";" - } else { - $changed += $parentExtentText.Substring($endOffset, $parentExtentText.Length - $endOffset) - } - } - - $NPX_NO_REDIRECTS_COMMAND += $changed - $prevEndOffset = $endOffset - $i++ - } - - $NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() - } else { - $NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() - } + $NPX_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPX_OG_COMMAND, [ref] $null, [ref] $null). + EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' ' + $NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" } From fdbbc4efdb910dc6b3b63e44160a76fed9c4c35b Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Tue, 27 May 2025 12:56:46 -0400 Subject: [PATCH 7/7] OG -> ORIGINAL --- bin/npm.ps1 | 6 +++--- bin/npx.ps1 | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index b5ce953b71552..5993adaf55662 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -28,9 +28,9 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input & $NODE_EXE $NPM_CLI_JS $args } else { # used "-Command" argument if ($MyInvocation.Statement) { - $NPM_OG_COMMAND = $MyInvocation.Statement + $NPM_ORIGINAL_COMMAND = $MyInvocation.Statement } else { - $NPM_OG_COMMAND = ( + $NPM_ORIGINAL_COMMAND = ( [Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic') ).GetValue($MyInvocation).Text } @@ -38,7 +38,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") - $NPM_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPM_OG_COMMAND, [ref] $null, [ref] $null). + $NPM_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPM_ORIGINAL_COMMAND, [ref] $null, [ref] $null). EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' ' $NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 59fe3c1abfbf7..cc1aa047bdc21 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -28,9 +28,9 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input & $NODE_EXE $NPX_CLI_JS $args } else { # used "-Command" argument if ($MyInvocation.Statement) { - $NPX_OG_COMMAND = $MyInvocation.Statement + $NPX_ORIGINAL_COMMAND = $MyInvocation.Statement } else { - $NPX_OG_COMMAND = ( + $NPX_ORIGINAL_COMMAND = ( [Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic') ).GetValue($MyInvocation).Text } @@ -38,7 +38,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") - $NPX_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPX_OG_COMMAND, [ref] $null, [ref] $null). + $NPX_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPX_ORIGINAL_COMMAND, [ref] $null, [ref] $null). EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' ' $NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim()