10
10
var execSync = require ( 'child_process' ) . execSync ;
11
11
var opn = require ( 'opn' ) ;
12
12
13
+ // https://github.com/sindresorhus/opn#app
14
+ var OSX_CHROME = 'google chrome' ;
15
+
13
16
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 ) {
15
39
try {
16
40
// Try our best to reuse existing tab
17
41
// on OS X Google Chrome with AppleScript
@@ -25,11 +49,12 @@ function openBrowser(url) {
25
49
// Ignore errors.
26
50
}
27
51
}
52
+
28
53
// Fallback to opn
29
54
// (It will always open new tab)
30
55
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.
33
58
return true ;
34
59
} catch ( err ) {
35
60
return false ;
0 commit comments