From 7dceecb1b72d3e75f4b745dd642052dc8aaaeb14 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Fri, 16 May 2025 10:27:53 -0400 Subject: [PATCH 1/7] fix(powershell): don't use Invoke-Expression if `ExpectingInput` --- bin/npm.ps1 | 11 ++--------- bin/npx.ps1 | 11 ++--------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 77bc9a5777c80..969b28ed4f244 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -22,7 +22,7 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { $NPM_CLI_JS=$NPM_PREFIX_NPM_CLI_JS } -if ($MyInvocation.Line) { # used "-Command" argument +if (-not $MyInvocation.ExpectingInput -and $MyInvocation.Line) { # used "-Command" argument if ($MyInvocation.Statement) { $NPM_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim() } else { @@ -35,14 +35,7 @@ if ($MyInvocation.Line) { # used "-Command" argument $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") - # Support pipeline input - if ($MyInvocation.ExpectingInput) { - $input = (@($input) -join "`n").Replace("``", "````") - - Invoke-Expression "Write-Output `"$input`" | & `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" - } else { - Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" - } + Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" } else { # used "-File" argument # Support pipeline input if ($MyInvocation.ExpectingInput) { diff --git a/bin/npx.ps1 b/bin/npx.ps1 index e89536bf3542a..4e0a3bf3b1b89 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -22,7 +22,7 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { $NPX_CLI_JS=$NPM_PREFIX_NPX_CLI_JS } -if ($MyInvocation.Line) { # used "-Command" argument +if (-not $MyInvocation.ExpectingInput -and $MyInvocation.Line) { # used "-Command" argument if ($MyInvocation.Statement) { $NPX_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim() } else { @@ -35,14 +35,7 @@ if ($MyInvocation.Line) { # used "-Command" argument $NODE_EXE = $NODE_EXE.Replace("``", "````") $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") - # Support pipeline input - if ($MyInvocation.ExpectingInput) { - $input = (@($input) -join "`n").Replace("``", "````") - - Invoke-Expression "Write-Output `"$input`" | & `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" - } else { - Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" - } + Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" } else { # used "-File" argument # Support pipeline input if ($MyInvocation.ExpectingInput) { From 3e34e163e31c43b35d407282af3d816aabfa7c06 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Tue, 20 May 2025 07:12:09 -0400 Subject: [PATCH 2/7] reorganize if statements --- bin/npm.ps1 | 13 +++++-------- bin/npx.ps1 | 13 +++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 969b28ed4f244..c4c8d9ec79943 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -22,7 +22,11 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { $NPM_CLI_JS=$NPM_PREFIX_NPM_CLI_JS } -if (-not $MyInvocation.ExpectingInput -and $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() } else { @@ -36,13 +40,6 @@ if (-not $MyInvocation.ExpectingInput -and $MyInvocation.Line) { # used "-Comman $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") 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 - } else { - & $NODE_EXE $NPM_CLI_JS $args - } } exit $LASTEXITCODE diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 4e0a3bf3b1b89..6ac637ff16498 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -22,7 +22,11 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { $NPX_CLI_JS=$NPM_PREFIX_NPX_CLI_JS } -if (-not $MyInvocation.ExpectingInput -and $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() } else { @@ -36,13 +40,6 @@ if (-not $MyInvocation.ExpectingInput -and $MyInvocation.Line) { # used "-Comman $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") 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 - } else { - & $NODE_EXE $NPX_CLI_JS $args - } } exit $LASTEXITCODE From 625f46b5611047cbe932a2c9ffb0ea6c0ba563d9 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Wed, 21 May 2025 11:25:00 -0400 Subject: [PATCH 3/7] warn when `$args` contains array in pipeline input mode --- bin/npm.ps1 | 3 +++ bin/npx.ps1 | 3 +++ 2 files changed, 6 insertions(+) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index c4c8d9ec79943..6d0b8f870640b 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -23,6 +23,9 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { } if ($MyInvocation.ExpectingInput) { # takes pipeline input + if ($args | Where-Object { $_ -is [array] }) { + echo "WARNING: arguments passed contains array" + } $input | & $NODE_EXE $NPM_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument & $NODE_EXE $NPM_CLI_JS $args diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 6ac637ff16498..bc266cea1b596 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -23,6 +23,9 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { } if ($MyInvocation.ExpectingInput) { # takes pipeline input + if ($args | Where-Object { $_ -is [array] }) { + echo "WARNING: arguments passed contains array" + } $input | & $NODE_EXE $NPX_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument & $NODE_EXE $NPX_CLI_JS $args From 4fc09586475925b6fb7c6d6e01ce2b84424940d2 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Wed, 21 May 2025 13:11:00 -0400 Subject: [PATCH 4/7] be more descriptive --- bin/npm.ps1 | 2 +- bin/npx.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 6d0b8f870640b..1fc94aed6f2ca 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -24,7 +24,7 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { if ($MyInvocation.ExpectingInput) { # takes pipeline input if ($args | Where-Object { $_ -is [array] }) { - echo "WARNING: arguments passed contains array" + echo "WARNING: arguments passed to npm contains array" } $input | & $NODE_EXE $NPM_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument diff --git a/bin/npx.ps1 b/bin/npx.ps1 index bc266cea1b596..1d87003abbeb6 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -24,7 +24,7 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { if ($MyInvocation.ExpectingInput) { # takes pipeline input if ($args | Where-Object { $_ -is [array] }) { - echo "WARNING: arguments passed contains array" + echo "WARNING: arguments passed to npx contains array" } $input | & $NODE_EXE $NPX_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument From fd31a9566ac986fe5d9c26157fb376bbfef8a919 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Wed, 21 May 2025 13:32:02 -0400 Subject: [PATCH 5/7] uppercase --- bin/npm.ps1 | 2 +- bin/npx.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 1fc94aed6f2ca..02cb872b2b67d 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -24,7 +24,7 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { if ($MyInvocation.ExpectingInput) { # takes pipeline input if ($args | Where-Object { $_ -is [array] }) { - echo "WARNING: arguments passed to npm contains array" + echo "WARNING: arguments passed to NPM contains array" } $input | & $NODE_EXE $NPM_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 1d87003abbeb6..74d9f856d262e 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -24,7 +24,7 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { if ($MyInvocation.ExpectingInput) { # takes pipeline input if ($args | Where-Object { $_ -is [array] }) { - echo "WARNING: arguments passed to npx contains array" + echo "WARNING: arguments passed to NPX contains array" } $input | & $NODE_EXE $NPX_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument From fda4c42419b144321262547f06f157e768965d52 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Wed, 21 May 2025 13:54:37 -0400 Subject: [PATCH 6/7] remove warning since it doesn't cover all cases --- bin/npm.ps1 | 3 --- bin/npx.ps1 | 3 --- 2 files changed, 6 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 02cb872b2b67d..c4c8d9ec79943 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -23,9 +23,6 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) { } if ($MyInvocation.ExpectingInput) { # takes pipeline input - if ($args | Where-Object { $_ -is [array] }) { - echo "WARNING: arguments passed to NPM contains array" - } $input | & $NODE_EXE $NPM_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument & $NODE_EXE $NPM_CLI_JS $args diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 74d9f856d262e..6ac637ff16498 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -23,9 +23,6 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) { } if ($MyInvocation.ExpectingInput) { # takes pipeline input - if ($args | Where-Object { $_ -is [array] }) { - echo "WARNING: arguments passed to NPX contains array" - } $input | & $NODE_EXE $NPX_CLI_JS $args } elseif (-not $MyInvocation.Line) { # used "-File" argument & $NODE_EXE $NPX_CLI_JS $args From 98e07180864a0620dca3f6041c046079b81be053 Mon Sep 17 00:00:00 2001 From: Alex Schwartz Date: Wed, 21 May 2025 17:51:32 -0400 Subject: [PATCH 7/7] [draft] don't allow ">" for new mode --- bin/npm.ps1 | 10 +++++++--- bin/npx.ps1 | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index c4c8d9ec79943..4add6e642b82f 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -36,10 +36,14 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } - $NODE_EXE = $NODE_EXE.Replace("``", "````") - $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") + if ($NPM_ARGS.Contains(">")) { + & $NODE_EXE $NPM_CLI_JS $args + } else { + $NODE_EXE = $NODE_EXE.Replace("``", "````") + $NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````") - Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" + Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS" + } } exit $LASTEXITCODE diff --git a/bin/npx.ps1 b/bin/npx.ps1 index 6ac637ff16498..ba9035d7a6c9f 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -36,10 +36,14 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input $NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim() } - $NODE_EXE = $NODE_EXE.Replace("``", "````") - $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") + if ($NPX_ARGS.Contains(">")) { + & $NODE_EXE $NPX_CLI_JS $args + } else { + $NODE_EXE = $NODE_EXE.Replace("``", "````") + $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````") - Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" + Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS" + } } exit $LASTEXITCODE