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

Implemented browser and browserPrivate #294

Merged
merged 24 commits into from Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8b11819
Added browser and browserPrivate under open.apps
leslieyip02 Jan 27, 2023
7253ffc
Switched default browser library
leslieyip02 Jan 30, 2023
5376f3d
Updated docs
leslieyip02 Jan 30, 2023
cc68d5a
Fixed issue with __dirname
leslieyip02 Jan 30, 2023
5828be0
Fix: removed ava test from test script
leslieyip02 Jan 30, 2023
4cf1a6d
Fix the `app` argument with WSL (#295)
kazarmy Jan 30, 2023
5c6582a
Removed unnecessary url import
leslieyip02 Feb 1, 2023
53bb565
Changed exports and specified supported browsers
leslieyip02 Feb 1, 2023
051edca
Fix `allowNonzeroExitCode` option (#296)
xirzec Feb 8, 2023
13a800c
Meta tweaks
sindresorhus Feb 8, 2023
27e4e3a
8.4.1
sindresorhus Feb 8, 2023
51fae87
Fix support for Podman
sindresorhus Feb 20, 2023
cbc008b
8.4.2
sindresorhus Feb 20, 2023
b3212fb
Mapped browser IDs to supported browsers
leslieyip02 Mar 13, 2023
aa21cad
Added browser and browserPrivate under open.apps
leslieyip02 Jan 27, 2023
f7b4c6d
Switched default browser library
leslieyip02 Jan 30, 2023
238770d
Updated docs
leslieyip02 Jan 30, 2023
009b28e
Fixed issue with __dirname
leslieyip02 Jan 30, 2023
69b4bb5
Fix: removed ava test from test script
leslieyip02 Jan 30, 2023
e368f9b
Removed unnecessary url import
leslieyip02 Feb 1, 2023
1af76c2
Changed exports and specified supported browsers
leslieyip02 Feb 1, 2023
a4867a4
Mapped browser IDs to supported browsers
leslieyip02 Mar 13, 2023
b185554
Merge branch 'main' of https://github.com/leslieyip02/open
leslieyip02 Mar 19, 2023
6d9d524
Added documentation for apps export
leslieyip02 Mar 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.d.ts
Expand Up @@ -90,7 +90,7 @@ declare const open: {

@example
```
import {open} from 'open';
import open from 'open';

// Opens the image in the default image viewer.
await open('unicorn.png', {wait: true});
Expand Down Expand Up @@ -119,7 +119,7 @@ declare const open: {

@example
```
import {open} from 'open';
import open from 'open';

await open('https://google.com', {
app: {
Expand All @@ -140,7 +140,7 @@ declare const open: {

@example
```
import {open} from 'open';
import open from 'open';
const {apps, openApp} = open;

// Open Firefox.
Expand Down
43 changes: 22 additions & 21 deletions index.js
Expand Up @@ -101,20 +101,8 @@ const baseOpen = async options => {
}));
}

if (app === 'browser') {
const browser = await defaultBrowser();
const browserName = browser.name.toLowerCase();
return baseOpen({
...options,
app: {
name: open.apps[browserName],
arguments: appArguments
}
});
}

if (app === 'browserPrivate') {
// Incognito or equivalent flags for each of the browser in open.apps
if (app === 'browser' || app === 'browserPrivate') {
// Incognito or equivalent flags for each browser in open.apps
const flags = {
chrome: '--incognito',
firefox: '--private-window',
Expand All @@ -123,13 +111,25 @@ const baseOpen = async options => {

const browser = await defaultBrowser();
const browserName = browser.name.toLowerCase();
return baseOpen({
...options,
app: {
name: open.apps[browserName],
arguments: [...appArguments, flags[browserName]]

const supportedBrowsers = Object.keys(flags);
for (const supportedBrowser of supportedBrowsers) {
if (browserName.includes(supportedBrowser)) {
if (app === 'browserPrivate') {
appArguments.push(flags[supportedBrowser]);
}

return baseOpen({
...options,
app: {
name: open.apps[supportedBrowser],
arguments: appArguments
}
});
}
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not too sure how to normalise the names, but is this good enough? It just checks if the browserName string contains "chrome", "firefox" or "edge" since these are the only browsers supported right now.

Copy link
Owner

Choose a reason for hiding this comment

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

That is not going to work. What if the default browser is Chrome Canary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the lack of replies. I don't know what to do besides converting to lowercase and stripping whitespace. Do you have any advice for normalisation?

Copy link
Owner

Choose a reason for hiding this comment

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

A list of browsers with their mapping on the different operating systems. We only support 3 browsers for this, so should not be hard to make a list like that.

For example, .chrome is Google Chrome on macOS, google-chrome on Linux.


throw new Error(`${browserName} as the default browser is not supported`);
}

let command;
Expand Down Expand Up @@ -350,4 +350,5 @@ defineLazyProperty(apps, 'browserPrivate', () => 'browserPrivate');
open.apps = apps;
open.openApp = openApp;
Copy link
Owner

Choose a reason for hiding this comment

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

These should be named exports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean something like this?

export {apps};
export default open;

Copy link
Owner

Choose a reason for hiding this comment

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

Yes


export {open};
export {apps};
export default open;
6 changes: 4 additions & 2 deletions readme.md
Expand Up @@ -26,7 +26,7 @@ npm install open
## Usage

```js
import {open} from 'open';
import open from 'open';

// Opens the image in the default image viewer and waits for the opened app to quit.
await open('unicorn.png', {wait: true});
Expand Down Expand Up @@ -124,7 +124,7 @@ We do not recommend setting this option. The convention for success is exit code
An object containing auto-detected binary names for common apps. Useful to work around [cross-platform differences](#app).

```js
import {open} from 'open';
import open from 'open';

await open('https://google.com', {
app: {
Expand All @@ -142,6 +142,8 @@ await open('https://google.com', {
- `browser` - Default web browser
- `browserPrivate` - Default web browser in incognito mode
Copy link
Owner

Choose a reason for hiding this comment

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

This needs to document which browsers it supports.


`browser` and `browserPrivate` currently support `chrome`, `firefox` and `edge`.

### open.openApp(name, options?)

Open an app.
Expand Down
2 changes: 1 addition & 1 deletion test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import {open} from './index.js';
import open from './index.js';
const {openApp} = open;

// Tests only checks that opening doesn't return an error
Expand Down