Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

powershell scripts for installed binaries #20

Closed
wants to merge 4 commits into from
Closed
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
57 changes: 57 additions & 0 deletions accepted/0000-powershell-bins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Powershell for Installed Binaries

AKA Terminate Terminate Batch Job

## Summary

Today, installing binaries using npm creates an executable bash script and a .cmd file with the same name. Unfortunately, terminating cmd scripts with SIGINT causes a useless prompt for windows users - "Terminate batch job (Y/N)". Also, Powershell is the default shell for Windows. By creating .ps1 scripts in addition to bash and .cmd, powershell users will have a better command line experience.

## Motivation

Windows users have three main options when it comes to shells:

- cmd: essentially unchanged from early versions of Windows
- powershell: the default Windows shell, under active development
- unixy shell: for example git-bash, zsh via WSL

WSL is growing in popularity, but isn't super common. Many Windows users need to use or prefer using a native shell. And for those using Powershell, we can make their experience better by creating powershell binary wrappers.
witty67 marked this conversation as resolved.
Show resolved Hide resolved

## Detailed Explanation

When installing binaries (e.g. `npm install -g typescript`), NPM should create `tsc.ps1` in addition to `tsc.cmd` and the executable `tsc` bash script. Powershell will prioritize `foo.ps1` over `foo.cmd` when users type `foo` on the command line. cmd users will have to continue living with it. Users of git-bash or WSL shells will be unaffected.

## Rationale and Alternatives

The main alternative is to maintain the status quo, but this leaves an annoying SIGINT prompt in Windows's default shell.

Creating just .ps1 scripts isn't an option. cmd can't start treating .ps1 scripts as executables (at least not by default), and NPM can't drop support for cmd.

## Implementation

Here's an example of a functionally identical powershell wrapper:

```powershell
$myPath = Split-Path $myInvocation.MyCommand.Path -Parent
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a difference between this and $PSScriptRoot?

Copy link

@joeyaiello joeyaiello Aug 22, 2018

Choose a reason for hiding this comment

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

PM for PowerShell here, @bterlson reached out to me on this one.

Citing this SO article, if you care about support on Windows 7 or Server 2008 R2 where PowerShell has not been updated past the inbox PowerShell 2.0, you should stick with $myInvocation.

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 @joeyaiello! So IMO it's probably better to err on the side of caution and go with the more compatible $myInvocation.

$node = Join-Path $myPath 'node.exe'
$binPath = Join-Path $myPath '[ path to node_module bin script, e.g. node_modules\typescript\bin\tsc]'
if (Test-Path $node)
{
& $node $binPath $args
}
else
{
node $binPath $args
}
```

The path to the bin script will have to be substituted into the right place.

## Prior Art

- [cmd-shim](https://www.npmjs.com/package/cmd-shim)
- [yarn PR](https://github.com/yarnpkg/yarn/pull/6093)

## Unresolved Questions and Bikeshedding

- impact of additional scripts on install times, node_modules size, etc.
- default powershell permissions: will powershell users need to toggle some settings so this works without permissions errors? Can the errors be worked around? (This PR will need testing on a fresh Windows install)