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
Changes from 5 commits
8b11819
7253ffc
5376f3d
cc68d5a
5828be0
4cf1a6d
5c6582a
53bb565
051edca
13a800c
27e4e3a
51fae87
cbc008b
b3212fb
aa21cad
f7b4c6d
238770d
009b28e
69b4bb5
e368f9b
1af76c2
a4867a4
b185554
6d9d524
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,11 +1,14 @@ | ||||||
const path = require('path'); | ||||||
const childProcess = require('child_process'); | ||||||
const {promises: fs, constants: fsConstants} = require('fs'); | ||||||
const isWsl = require('is-wsl'); | ||||||
const isDocker = require('is-docker'); | ||||||
const defineLazyProperty = require('define-lazy-prop'); | ||||||
import path from 'path'; | ||||||
import {fileURLToPath} from 'url'; | ||||||
import childProcess from 'child_process'; | ||||||
import {promises as fs, constants as fsConstants} from 'fs'; | ||||||
import isWsl from 'is-wsl'; | ||||||
import isDocker from 'is-docker'; | ||||||
import defineLazyProperty from 'define-lazy-prop'; | ||||||
import defaultBrowser from 'default-browser'; | ||||||
|
||||||
// Path to included `xdg-open`. | ||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||||||
const localXdgOpenPath = path.join(__dirname, 'xdg-open'); | ||||||
|
||||||
const {platform, arch} = process; | ||||||
|
@@ -98,6 +101,37 @@ 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 | ||||||
const flags = { | ||||||
chrome: '--incognito', | ||||||
firefox: '--private-window', | ||||||
edge: '--inPrivate' | ||||||
}; | ||||||
|
||||||
const browser = await defaultBrowser(); | ||||||
const browserName = browser.name.toLowerCase(); | ||||||
return baseOpen({ | ||||||
...options, | ||||||
app: { | ||||||
name: open.apps[browserName], | ||||||
arguments: [...appArguments, flags[browserName]] | ||||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
let command; | ||||||
const cliArguments = []; | ||||||
const childProcessOptions = {}; | ||||||
|
@@ -130,7 +164,7 @@ const baseOpen = async options => { | |||||
cliArguments.push( | ||||||
'-NoProfile', | ||||||
'-NonInteractive', | ||||||
'–ExecutionPolicy', | ||||||
'-ExecutionPolicy', | ||||||
'Bypass', | ||||||
'-EncodedCommand' | ||||||
); | ||||||
|
@@ -148,17 +182,17 @@ const baseOpen = async options => { | |||||
if (app) { | ||||||
// Double quote with double quotes to ensure the inner quotes are passed through. | ||||||
// Inner quotes are delimited for PowerShell interpretation with backticks. | ||||||
encodedArguments.push(`"\`"${app}\`""`, '-ArgumentList'); | ||||||
encodedArguments.push(`"\`"${app}\`""`); | ||||||
if (options.target) { | ||||||
appArguments.unshift(options.target); | ||||||
appArguments.push(options.target); | ||||||
} | ||||||
} else if (options.target) { | ||||||
encodedArguments.push(`"${options.target}"`); | ||||||
} | ||||||
|
||||||
if (appArguments.length > 0) { | ||||||
appArguments = appArguments.map(arg => `"\`"${arg}\`""`); | ||||||
encodedArguments.push(appArguments.join(',')); | ||||||
encodedArguments.push('-ArgumentList', appArguments.join(',')); | ||||||
} | ||||||
|
||||||
// Using Base64-encoded command, accepted by PowerShell, to allow special characters. | ||||||
|
@@ -309,7 +343,11 @@ defineLazyProperty(apps, 'edge', () => detectPlatformBinary({ | |||||
wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe' | ||||||
})); | ||||||
|
||||||
defineLazyProperty(apps, 'browser', () => 'browser'); | ||||||
|
||||||
defineLazyProperty(apps, 'browserPrivate', () => 'browserPrivate'); | ||||||
|
||||||
open.apps = apps; | ||||||
open.openApp = openApp; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should be named exports. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean something like this? export {apps};
export default open; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||||||
|
||||||
module.exports = open; | ||||||
export {open}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"name": "open", | ||
"version": "8.4.0", | ||
"type": "module", | ||
"description": "Open stuff like URLs, files, executables. Cross-platform.", | ||
"license": "MIT", | ||
"repository": "sindresorhus/open", | ||
|
@@ -50,7 +51,9 @@ | |
"dependencies": { | ||
"define-lazy-prop": "^2.0.0", | ||
"is-docker": "^2.1.1", | ||
"is-wsl": "^2.2.0" | ||
"is-wsl": "^2.2.0", | ||
"url": "^0.11.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"default-browser": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^15.0.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ npm install open | |
## Usage | ||
|
||
```js | ||
const open = require('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}); | ||
|
@@ -41,10 +41,13 @@ await open('https://sindresorhus.com', {app: {name: 'firefox'}}); | |
// Specify app arguments. | ||
await open('https://sindresorhus.com', {app: {name: 'google chrome', arguments: ['--incognito']}}); | ||
|
||
// Open an app | ||
// Opens the URL in the default browser in incognito mode. | ||
await open('https://sindresorhus.com', {app: {name: open.apps.browserPrivate}}); | ||
|
||
// Open an app. | ||
await open.openApp('xcode'); | ||
|
||
// Open an app with arguments | ||
// Open an app with arguments. | ||
await open.openApp(open.apps.chrome, {arguments: ['--incognito']}); | ||
``` | ||
|
||
|
@@ -121,20 +124,23 @@ 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 | ||
const open = require('open'); | ||
import {open} from 'open'; | ||
|
||
await open('https://google.com', { | ||
app: { | ||
name: open.apps.chrome | ||
} | ||
}); | ||
``` | ||
`browser` and `browserPrivate` can also be used to access the user's default browser through [`default-browser`](https://github.com/sindresorhus/default-browser). | ||
|
||
#### Supported apps | ||
|
||
- [`chrome`](https://www.google.com/chrome) - Web browser | ||
- [`firefox`](https://www.mozilla.org/firefox) - Web browser | ||
- [`edge`](https://www.microsoft.com/edge) - Web browser | ||
- `browser` - Default web browser | ||
- `browserPrivate` - Default web browser in incognito mode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to document which browsers it supports. |
||
|
||
### open.openApp(name, options?) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will need to do some normalisation of the names.
browserName
will be different depending on the OS.