-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start
script: reuse tab if possible in more Chromium browsers (#1036)
Co-authored-by: Adam Skoufis <askoufis@users.noreply.github.com>
- Loading branch information
1 parent
336fdd6
commit 16fdf5e
Showing
5 changed files
with
194 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
'sku': minor | ||
--- | ||
|
||
Widen support for reusing existing browser tab to more Chromium browsers. | ||
|
||
`start` and `start-ssr` scripts would previously only reuse an existing tab in Google Chrome. | ||
This change adds support for the following Chromium browsers: | ||
|
||
- Google Chrome, | ||
- Google Chrome Canary, | ||
- Microsoft Edge, | ||
- Brave Browser, | ||
- Vivaldi, | ||
- Chromium, | ||
- Arc. | ||
|
||
A tab will be reused if: | ||
|
||
* The OS is macOS, | ||
* The user's default browser is a supported Chromium browser, | ||
* The user has an existing tab open in a supported Chromium browser with the exact same URL. | ||
|
||
If any of the above is not true, a new tab will be created in the user's default browser. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,64 @@ | ||
// Inspired by create-react-app | ||
// https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/openBrowser.js | ||
// https://github.com/facebook/create-react-app/commit/d2de54b25cc25800df1764058997e3e274bd79ac | ||
|
||
const execSync = require('node:child_process').execSync; | ||
const defaultBrowser = require('x-default-browser'); | ||
const open = require('open'); | ||
|
||
const isCI = require('../isCI'); | ||
|
||
module.exports = (url) => { | ||
const OSX_CHROME = 'google chrome'; | ||
|
||
const supportedChromiumBrowsers = [ | ||
'Google Chrome', | ||
'Google Chrome Canary', | ||
'Microsoft Edge', | ||
'Brave Browser', | ||
'Vivaldi', | ||
'Chromium', | ||
'Arc', | ||
]; | ||
|
||
module.exports = async (url) => { | ||
if (process.env.OPEN_TAB !== 'false' && !isCI) { | ||
defaultBrowser((err, res) => { | ||
const useChrome = err ? false : res.isChrome; | ||
const { default: getDefaultBrowser } = await import('default-browser'); | ||
const { name: defaultBrowser } = await getDefaultBrowser(); | ||
|
||
const availableBrowser = process.env.BROWSER; | ||
|
||
const shouldTryOpenChromiumWithAppleScript = | ||
process.platform === 'darwin' && | ||
(typeof availableBrowser !== 'string' || | ||
availableBrowser === OSX_CHROME) && | ||
supportedChromiumBrowsers.includes(defaultBrowser); | ||
|
||
if (shouldTryOpenChromiumWithAppleScript) { | ||
// Will use the first open browser found from list | ||
const supportedChromiumBrowsersByPreference = new Set([ | ||
defaultBrowser, | ||
...supportedChromiumBrowsers, | ||
]); | ||
|
||
if (process.platform === 'darwin' && useChrome) { | ||
for (const chromiumBrowser of supportedChromiumBrowsersByPreference) { | ||
try { | ||
// Try our best to reuse existing tab | ||
// on OS X Google Chrome with AppleScript | ||
execSync('ps cax | grep "Google Chrome"'); | ||
execSync(`osascript openChrome.applescript "${encodeURI(url)}"`, { | ||
cwd: __dirname, | ||
stdio: 'ignore', | ||
}); | ||
return; | ||
execSync(`ps cax | grep "${chromiumBrowser}"`); | ||
execSync( | ||
`osascript openChrome.applescript "${encodeURI( | ||
url, | ||
)}" "${chromiumBrowser}"`, | ||
{ | ||
cwd: __dirname, | ||
stdio: 'ignore', | ||
}, | ||
); | ||
return true; | ||
} catch (e) { | ||
// Ignore errors. | ||
} | ||
} | ||
} | ||
|
||
open(url, { url: true }); | ||
}); | ||
open(url, { url: true }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.