Skip to content

Commit 519d32a

Browse files
authored
Only open Chrome tab if BROWSER is missing or is Chrome (facebook#1247)
1 parent 759806e commit 519d32a

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

packages/react-dev-utils/openBrowser.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,32 @@
1010
var execSync = require('child_process').execSync;
1111
var opn = require('opn');
1212

13+
// https://github.com/sindresorhus/opn#app
14+
var OSX_CHROME = 'google chrome';
15+
1316
function openBrowser(url) {
14-
if (process.platform === 'darwin') {
17+
// Attempt to honor this environment variable.
18+
// It is specific to the operating system.
19+
// See https://github.com/sindresorhus/opn#app for documentation.
20+
const browser = process.env.BROWSER;
21+
22+
// Special case: BROWSER="none" will prevent opening completely.
23+
if (browser === 'none') {
24+
return false;
25+
}
26+
27+
// If we're on OS X, the user hasn't specifically
28+
// requested a different browser, we can try opening
29+
// Chrome with AppleScript. This lets us reuse an
30+
// existing tab when possible instead of creating a new one.
31+
const shouldTryOpenChromeWithAppleScript = (
32+
process.platform === 'darwin' && (
33+
typeof browser !== 'string' ||
34+
browser === OSX_CHROME
35+
)
36+
);
37+
38+
if (shouldTryOpenChromeWithAppleScript) {
1539
try {
1640
// Try our best to reuse existing tab
1741
// on OS X Google Chrome with AppleScript
@@ -25,11 +49,12 @@ function openBrowser(url) {
2549
// Ignore errors.
2650
}
2751
}
52+
2853
// Fallback to opn
2954
// (It will always open new tab)
3055
try {
31-
var option = {app: process.env.BROWSER};
32-
opn(url, option).catch(() => {}); // Prevent `unhandledRejection` error.
56+
var options = {app: browser};
57+
opn(url, options).catch(() => {}); // Prevent `unhandledRejection` error.
3358
return true;
3459
} catch (err) {
3560
return false;

0 commit comments

Comments
 (0)