Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bin/npm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ if (Test-Path $NPM_PREFIX_NPM_CLI_JS) {
$NPM_CLI_JS=$NPM_PREFIX_NPM_CLI_JS
}

$NPM_ARGS = $MyInvocation.Line.Substring($MyInvocation.InvocationName.Length).Trim()
$INVOKE_NPM = "$($NODE_EXE -Replace ' ', '` ') $($NPM_CLI_JS -Replace ' ', '` ') $NPM_ARGS".Trim()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be absolutely sure this won't overlap w/ npm's escaping when it spawns scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted to draft as I’m still experimenting with this.

Copy link

@noseratio noseratio May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukekarrys also please don't use MyInvocation.Line, use MyInvocation.Statement instead. Here's why:

If npm.ps1 is invoked as part of a multi-statement one-liner, like echo "Hello" && npm start -- commmand --arg=value, then MyInvocation props would look like this:

  "Line": "echo \"Hello\" && npm start -- commmand --arg=value",
  "Statement": "npm start -- commmand --arg=value",
  "InvocationName": "npm",

PS Thanks for a shoutout! 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip @noseratio. I used MyInvocation.Line in a force-push after I couldn't get MyInvocation.Statement working. In the tests these files are called directly via child_process.spawn so I'm not sure if that is affecting them. I will need to do some more research to see why both Line and Statement are not available on MyInvocation in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But MyInvocation.Statement did work for me locally.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also interestingly, if npm is typed in a Git-Bash prompt on Windows, the Bash version - "C:\Program Files\nodejs\npm" - will get invoked.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last but not least, npm.cmd remains being super important, as it will be invoked for nested npm scripts (like startapp below), regardless of the outer shell:

package.json:

{
  "name": "cli-test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "startapp": "npm start -- ",
    "showcli": "echo"
  }
}
C:\temp\cli-test>pwsh –noprofile                                                                                                      PowerShell 7.4.2
PS C:\temp\cli-test> npm run startapp -- command --arg=value
Hello from npm.ps1

> cli-test@1.0.0 startapp
> npm start -- command --arg=value
Hello from npm.bat

> cli-test@1.0.0 start
> node index.js command --arg=value

[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\temp\\cli-test\\index.js',
  'command',
  '--arg=value'
]
PS C:\temp\cli-test> & "C:\Program Files\Git\bin\bash.exe"

Master@i3msi MINGW64 /c/temp/cli-test
$ npm run startapp -- command --arg=value
Hello from npm[.sh]

> cli-test@1.0.0 startapp
> npm start -- command --arg=value

Hello from npm.bat

> cli-test@1.0.0 start
> node index.js command --arg=value

[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\temp\\cli-test\\index.js',
  'command',
  '--arg=value'
]

That can probably only be changed by overriding the shell in .npmrc:

shell = "pwsh"

Copy link
Contributor

@alexsch01 alexsch01 May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noseratio

also please don't use MyInvocation.Line, use MyInvocation.Statement instead.

On my Windows PowerShell, MyInvocation.Statement doesn't exist.
In #8267, I chose to use MyInvocation.Line due to that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be conditional. "Line" does not work when multiple statements are joined which is common like " echo 'test' & npm run my-script".

An additional challenge is how we test using spawn. In my quick try, neither $MyInnvocation.Statement nor Line are populated.

We can get this done but it will take some tries.

Copy link
Contributor

@alexsch01 alexsch01 May 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbtools fixed multiple statements in latest commit of the new PR
unforunately $MyInvocation.Position is not populated either


# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $NODE_EXE $NPM_CLI_JS $args
$input | Invoke-Expression $INVOKE_NPM
} else {
& $NODE_EXE $NPM_CLI_JS $args
Invoke-Expression $INVOKE_NPM
}

exit $LASTEXITCODE
7 changes: 5 additions & 2 deletions bin/npx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ if (Test-Path $NPM_PREFIX_NPX_CLI_JS) {
$NPX_CLI_JS=$NPM_PREFIX_NPX_CLI_JS
}

$NPX_ARGS = $MyInvocation.Line.Substring($MyInvocation.InvocationName.Length).Trim()
$INVOKE_NPX = "$($NODE_EXE -Replace ' ', '` ') $($NPX_CLI_JS -Replace ' ', '` ') $NPX_ARGS".Trim()

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $NODE_EXE $NPX_CLI_JS $args
$input | Invoke-Expression $INVOKE_NPX
} else {
& $NODE_EXE $NPX_CLI_JS $args
Invoke-Expression $INVOKE_NPX
}

exit $LASTEXITCODE