1010var chalk = require ( 'colors-cli' ) ;
1111var execSync = require ( 'child_process' ) . execSync ;
1212var spawn = require ( 'cross-spawn' ) ;
13- var opn = require ( 'opn ' ) ;
13+ var open = require ( 'open ' ) ;
1414
15- // https://github.com/sindresorhus/opn #app
15+ // https://github.com/sindresorhus/open #app
1616var OSX_CHROME = 'google chrome' ;
1717
1818const Actions = Object . freeze ( {
@@ -24,8 +24,11 @@ const Actions = Object.freeze({
2424function getBrowserEnv ( ) {
2525 // Attempt to honor this environment variable.
2626 // It is specific to the operating system.
27- // See https://github.com/sindresorhus/opn #app for documentation.
27+ // See https://github.com/sindresorhus/open #app for documentation.
2828 const value = process . env . BROWSER ;
29+ const args = process . env . BROWSER_ARGS
30+ ? process . env . BROWSER_ARGS . split ( ' ' )
31+ : [ ] ;
2932 let action ;
3033 if ( ! value ) {
3134 // Default.
@@ -37,7 +40,7 @@ function getBrowserEnv() {
3740 } else {
3841 action = Actions . BROWSER ;
3942 }
40- return { action, value } ;
43+ return { action, value, args } ;
4144}
4245
4346function executeNodeScript ( scriptPath , url ) {
@@ -61,7 +64,7 @@ function executeNodeScript(scriptPath, url) {
6164 return true ;
6265}
6366
64- function startBrowserProcess ( browser , url ) {
67+ function startBrowserProcess ( browser , url , args ) {
6568 // If we're on OS X, the user hasn't specifically
6669 // requested a different browser, we can try opening
6770 // Chrome with AppleScript. This lets us reuse an
@@ -93,34 +96,39 @@ function startBrowserProcess(browser, url) {
9396 browser = undefined ;
9497 }
9598
96- // Fallback to opn
99+ // If there are arguments, they must be passed as array with the browser
100+ if ( typeof browser === 'string' && args . length > 0 ) {
101+ browser = [ browser ] . concat ( args ) ;
102+ }
103+
104+ // Fallback to open
97105 // (It will always open new tab)
98106 try {
99- var options = { app : browser } ;
100- opn ( url , options ) . catch ( ( ) => { } ) ; // Prevent `unhandledRejection` error.
107+ var options = { app : browser , wait : false , url : true } ;
108+ open ( url , options ) . catch ( ( ) => { } ) ; // Prevent `unhandledRejection` error.
101109 return true ;
102110 } catch ( err ) {
103111 return false ;
104112 }
105113}
106114
107115/**
108- * Reads the BROWSER evironment variable and decides what to do with it. Returns
116+ * Reads the BROWSER environment variable and decides what to do with it. Returns
109117 * true if it opened a browser or ran a node.js script, otherwise false.
110118 */
111119function openBrowser ( url ) {
112- const { action, value } = getBrowserEnv ( ) ;
120+ const { action, value, args } = getBrowserEnv ( ) ;
113121 switch ( action ) {
114122 case Actions . NONE :
115123 // Special case: BROWSER="none" will prevent opening completely.
116124 return false ;
117125 case Actions . SCRIPT :
118126 return executeNodeScript ( value , url ) ;
119127 case Actions . BROWSER :
120- return startBrowserProcess ( value , url ) ;
128+ return startBrowserProcess ( value , url , args ) ;
121129 default :
122130 throw new Error ( 'Not implemented.' ) ;
123131 }
124132}
125133
126- module . exports = openBrowser ;
134+ module . exports = openBrowser ;
0 commit comments