10
10
var chalk = require ( 'colors-cli' ) ;
11
11
var execSync = require ( 'child_process' ) . execSync ;
12
12
var spawn = require ( 'cross-spawn' ) ;
13
- var opn = require ( 'opn ' ) ;
13
+ var open = require ( 'open ' ) ;
14
14
15
- // https://github.com/sindresorhus/opn #app
15
+ // https://github.com/sindresorhus/open #app
16
16
var OSX_CHROME = 'google chrome' ;
17
17
18
18
const Actions = Object . freeze ( {
@@ -24,8 +24,11 @@ const Actions = Object.freeze({
24
24
function getBrowserEnv ( ) {
25
25
// Attempt to honor this environment variable.
26
26
// 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.
28
28
const value = process . env . BROWSER ;
29
+ const args = process . env . BROWSER_ARGS
30
+ ? process . env . BROWSER_ARGS . split ( ' ' )
31
+ : [ ] ;
29
32
let action ;
30
33
if ( ! value ) {
31
34
// Default.
@@ -37,7 +40,7 @@ function getBrowserEnv() {
37
40
} else {
38
41
action = Actions . BROWSER ;
39
42
}
40
- return { action, value } ;
43
+ return { action, value, args } ;
41
44
}
42
45
43
46
function executeNodeScript ( scriptPath , url ) {
@@ -61,7 +64,7 @@ function executeNodeScript(scriptPath, url) {
61
64
return true ;
62
65
}
63
66
64
- function startBrowserProcess ( browser , url ) {
67
+ function startBrowserProcess ( browser , url , args ) {
65
68
// If we're on OS X, the user hasn't specifically
66
69
// requested a different browser, we can try opening
67
70
// Chrome with AppleScript. This lets us reuse an
@@ -93,34 +96,39 @@ function startBrowserProcess(browser, url) {
93
96
browser = undefined ;
94
97
}
95
98
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
97
105
// (It will always open new tab)
98
106
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.
101
109
return true ;
102
110
} catch ( err ) {
103
111
return false ;
104
112
}
105
113
}
106
114
107
115
/**
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
109
117
* true if it opened a browser or ran a node.js script, otherwise false.
110
118
*/
111
119
function openBrowser ( url ) {
112
- const { action, value } = getBrowserEnv ( ) ;
120
+ const { action, value, args } = getBrowserEnv ( ) ;
113
121
switch ( action ) {
114
122
case Actions . NONE :
115
123
// Special case: BROWSER="none" will prevent opening completely.
116
124
return false ;
117
125
case Actions . SCRIPT :
118
126
return executeNodeScript ( value , url ) ;
119
127
case Actions . BROWSER :
120
- return startBrowserProcess ( value , url ) ;
128
+ return startBrowserProcess ( value , url , args ) ;
121
129
default :
122
130
throw new Error ( 'Not implemented.' ) ;
123
131
}
124
132
}
125
133
126
- module . exports = openBrowser ;
134
+ module . exports = openBrowser ;
0 commit comments