Skip to content

Commit f7ff89c

Browse files
fix(powershell): fix StrictMode and improve argument parsing (#8551)
[v10 branch] Cherry-picks of 5d82d0b 5f18557 dd4cee9 --------- Co-authored-by: Aaron Jensen <[email protected]>
1 parent f69c403 commit f7ff89c

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

bin/npm.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env pwsh
22

3+
Set-StrictMode -Version 'Latest'
4+
35
$NODE_EXE="$PSScriptRoot/node.exe"
46
if (-not (Test-Path $NODE_EXE)) {
57
$NODE_EXE="$PSScriptRoot/node"
@@ -27,7 +29,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input
2729
} elseif (-not $MyInvocation.Line) { # used "-File" argument
2830
& $NODE_EXE $NPM_CLI_JS $args
2931
} else { # used "-Command" argument
30-
if ($MyInvocation.Statement) {
32+
if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {
3133
$NPM_ORIGINAL_COMMAND = $MyInvocation.Statement
3234
} else {
3335
$NPM_ORIGINAL_COMMAND = (
@@ -38,9 +40,9 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input
3840
$NODE_EXE = $NODE_EXE.Replace("``", "````")
3941
$NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````")
4042

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()
43+
$NPM_COMMAND_ARRAY = [Management.Automation.Language.Parser]::ParseInput($NPM_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
44+
EndBlock.Statements.PipelineElements.CommandElements.Extent.Text
45+
$NPM_ARGS = ($NPM_COMMAND_ARRAY | Select-Object -Skip 1) -join ' '
4446

4547
Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
4648
}

bin/npx.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env pwsh
22

3+
Set-StrictMode -Version 'Latest'
4+
35
$NODE_EXE="$PSScriptRoot/node.exe"
46
if (-not (Test-Path $NODE_EXE)) {
57
$NODE_EXE="$PSScriptRoot/node"
@@ -27,7 +29,7 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input
2729
} elseif (-not $MyInvocation.Line) { # used "-File" argument
2830
& $NODE_EXE $NPX_CLI_JS $args
2931
} else { # used "-Command" argument
30-
if ($MyInvocation.Statement) {
32+
if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {
3133
$NPX_ORIGINAL_COMMAND = $MyInvocation.Statement
3234
} else {
3335
$NPX_ORIGINAL_COMMAND = (
@@ -38,9 +40,9 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input
3840
$NODE_EXE = $NODE_EXE.Replace("``", "````")
3941
$NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````")
4042

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()
43+
$NPX_COMMAND_ARRAY = [Management.Automation.Language.Parser]::ParseInput($NPX_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
44+
EndBlock.Statements.PipelineElements.CommandElements.Extent.Text
45+
$NPX_ARGS = ($NPX_COMMAND_ARRAY | Select-Object -Skip 1) -join ' '
4446

4547
Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS"
4648
}

0 commit comments

Comments
 (0)