Skip to content

Commit dd4cee9

Browse files
authored
fix(powershell): improve argument parsing (#8539)
improve the argument parsing PS1 logic - support `& npm args` and `. npm args` properly - support syntax such as `C:\"Program Files"\nodejs\npm.ps1 args` **of course ^ for both npm and npx version of the script** Code Explanation: instead of getting the `CommandElements.Extent.Text` array and joining it with spaces right away, now it's getting the same array and only capturing everything after the first element
1 parent 5f18557 commit dd4cee9

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

bin/npm.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) {
2626

2727
if ($MyInvocation.ExpectingInput) { # takes pipeline input
2828
$input | & $NODE_EXE $NPM_CLI_JS $args
29-
} elseif (-not $MyInvocation.Line -or $MyInvocation.InvocationName -in '&', '.') { # used "-File" argument
29+
} elseif (-not $MyInvocation.Line) { # used "-File" argument
3030
& $NODE_EXE $NPM_CLI_JS $args
3131
} else { # used "-Command" argument
3232
if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {
@@ -40,9 +40,9 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input
4040
$NODE_EXE = $NODE_EXE.Replace("``", "````")
4141
$NPM_CLI_JS = $NPM_CLI_JS.Replace("``", "````")
4242

43-
$NPM_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPM_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
44-
EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' '
45-
$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 ' '
4646

4747
Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
4848
}

bin/npx.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) {
2626

2727
if ($MyInvocation.ExpectingInput) { # takes pipeline input
2828
$input | & $NODE_EXE $NPX_CLI_JS $args
29-
} elseif (-not $MyInvocation.Line -or $MyInvocation.InvocationName -in '&', '.') { # used "-File" argument
29+
} elseif (-not $MyInvocation.Line) { # used "-File" argument
3030
& $NODE_EXE $NPX_CLI_JS $args
3131
} else { # used "-Command" argument
3232
if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {
@@ -40,9 +40,9 @@ if ($MyInvocation.ExpectingInput) { # takes pipeline input
4040
$NODE_EXE = $NODE_EXE.Replace("``", "````")
4141
$NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````")
4242

43-
$NPX_NO_REDIRECTS_COMMAND = [Management.Automation.Language.Parser]::ParseInput($NPX_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
44-
EndBlock.Statements.PipelineElements.CommandElements.Extent.Text -join ' '
45-
$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 ' '
4646

4747
Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS"
4848
}

0 commit comments

Comments
 (0)