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

fix: open URL in browser on WSL #128

Merged
merged 4 commits into from
Oct 18, 2024
Merged

Conversation

mbtools
Copy link
Contributor

@mbtools mbtools commented Oct 16, 2024

Here's a more elegant solution for opening URLs from WSL in your favorite browser. It is based on sensible-browser which is included in the default distribution for WSL. This avoids issues with the WSL environment, cmd.exe. quoting parameters, etc.

In WSL, set the default browser using the BROWSER variable, for example,

export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
or
export BROWSER="/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"

Note: To permanently set the default browser, add the appropriate entry to your shell's RC file, e.g. .bashrc or .zshrc.

To launch a URL from the WSL command line:

sensible-browser https://google.com

To launch a URL using promise-spawn:

const promiseSpawn = require('@npmcli/promise-spawn')
promiseSpawn.open('https://google.com')

Replaces #118
Closes #62

Test

os: 5.15.153.1-microsoft-standard-WSL2
node: 20.18.0
npm: 10.8.2

image

@mbtools mbtools requested a review from a team as a code owner October 16, 2024 19:29
@wraithgar
Copy link
Member

What happens w/ this PR if the user has not set BROWSER?

test/open.js Outdated Show resolved Hide resolved
mbtools and others added 2 commits October 17, 2024 17:19
Co-authored-by: Gar <wraithgar@github.com>
@mbtools
Copy link
Contributor Author

mbtools commented Oct 17, 2024

If BROWSER is not set, it now rejects the promise with a message similar to what you get in WSL cli:

image

I adjusted the tests accordingly

@wraithgar wraithgar merged commit 5ecf301 into npm:main Oct 18, 2024
20 checks passed
@github-actions github-actions bot mentioned this pull request Oct 18, 2024
wraithgar pushed a commit that referenced this pull request Oct 18, 2024
🤖 I have created a release *beep* *boop*
---


## [8.0.2](v8.0.1...v8.0.2)
(2024-10-18)
### Bug Fixes
*
[`5ecf301`](5ecf301)
[#128](#128) open URL in
browser on WSL (#128) (@mbtools, @wraithgar)
### Chores
*
[`ef4ba09`](ef4ba09)
[#127](#127) bump
@npmcli/template-oss from 4.23.3 to 4.23.4 (#127) (@dependabot[bot],
@npm-cli-bot)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@mbtools mbtools deleted the wsl_open_browser branch October 23, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Probably does not work with WSL2
2 participants