1010var execSync = require ( 'child_process' ) . execSync ;
1111var opn = require ( 'opn' ) ;
1212
13+ // https://github.com/sindresorhus/opn#app
14+ var OSX_CHROME = 'google chrome' ;
15+
1316function 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