Skip to content

Commit e758dd7

Browse files
authored
fix(powershell): multiple Invoke-Expression fixes (#8318)
- prevent pipeline input from using Invoke-Expression - allow redirect operator to behave again by stripping redirects out from Invoke-Expression
1 parent 8b55d38 commit e758dd7

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

bin/npm.ps1

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,27 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) {
2222
$NPM_CLI_JS=$NPM_PREFIX_NPM_CLI_JS
2323
}
2424

25-
if ($MyInvocation.Line) { # used "-Command" argument
25+
if ($MyInvocation.ExpectingInput) { # takes pipeline input
26+
$input | & $NODE_EXE $NPM_CLI_JS $args
27+
} elseif (-not $MyInvocation.Line) { # used "-File" argument
28+
& $NODE_EXE $NPM_CLI_JS $args
29+
} else { # used "-Command" argument
2630
if ($MyInvocation.Statement) {
27-
$NPM_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim()
31+
$NPM_ORIGINAL_COMMAND = $MyInvocation.Statement
2832
} else {
29-
$NPM_OG_COMMAND = (
30-
[System.Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [System.Reflection.BindingFlags] 'Instance, NonPublic')
33+
$NPM_ORIGINAL_COMMAND = (
34+
[Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic')
3135
).GetValue($MyInvocation).Text
32-
$NPM_ARGS = $NPM_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim()
3336
}
3437

3538
$NODE_EXE = $NODE_EXE.Replace("``", "````")
3639
$NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````")
3740

38-
# Support pipeline input
39-
if ($MyInvocation.ExpectingInput) {
40-
$input = (@($input) -join "`n").Replace("``", "````")
41+
$NPM_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPM_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
42+
EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' '
43+
$NPM_ARGS = $NPM_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim()
4144

42-
Invoke-Expression "Write-Output `"$input`" | & `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
43-
} else {
44-
Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
45-
}
46-
} else { # used "-File" argument
47-
# Support pipeline input
48-
if ($MyInvocation.ExpectingInput) {
49-
$input | & $NODE_EXE $NPM_CLI_JS $args
50-
} else {
51-
& $NODE_EXE $NPM_CLI_JS $args
52-
}
45+
Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
5346
}
5447

5548
exit $LASTEXITCODE

bin/npx.ps1

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,27 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) {
2222
$NPX_CLI_JS=$NPM_PREFIX_NPX_CLI_JS
2323
}
2424

25-
if ($MyInvocation.Line) { # used "-Command" argument
25+
if ($MyInvocation.ExpectingInput) { # takes pipeline input
26+
$input | & $NODE_EXE $NPX_CLI_JS $args
27+
} elseif (-not $MyInvocation.Line) { # used "-File" argument
28+
& $NODE_EXE $NPX_CLI_JS $args
29+
} else { # used "-Command" argument
2630
if ($MyInvocation.Statement) {
27-
$NPX_ARGS = $MyInvocation.Statement.Substring($MyInvocation.InvocationName.Length).Trim()
31+
$NPX_ORIGINAL_COMMAND = $MyInvocation.Statement
2832
} else {
29-
$NPX_OG_COMMAND = (
30-
[System.Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [System.Reflection.BindingFlags] 'Instance, NonPublic')
33+
$NPX_ORIGINAL_COMMAND = (
34+
[Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic')
3135
).GetValue($MyInvocation).Text
32-
$NPX_ARGS = $NPX_OG_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim()
3336
}
3437

3538
$NODE_EXE = $NODE_EXE.Replace("``", "````")
3639
$NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````")
3740

38-
# Support pipeline input
39-
if ($MyInvocation.ExpectingInput) {
40-
$input = (@($input) -join "`n").Replace("``", "````")
41+
$NPX_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPX_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
42+
EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' '
43+
$NPX_ARGS = $NPX_NO_REDIRECTS_COMMAND.Substring($MyInvocation.InvocationName.Length).Trim()
4144

42-
Invoke-Expression "Write-Output `"$input`" | & `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS"
43-
} else {
44-
Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS"
45-
}
46-
} else { # used "-File" argument
47-
# Support pipeline input
48-
if ($MyInvocation.ExpectingInput) {
49-
$input | & $NODE_EXE $NPX_CLI_JS $args
50-
} else {
51-
& $NODE_EXE $NPX_CLI_JS $args
52-
}
45+
Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS"
5346
}
5447

5548
exit $LASTEXITCODE

0 commit comments

Comments
 (0)