9
9
*/
10
10
11
11
import { join } from 'node:path'
12
- import { execSync } from 'node:child_process'
12
+ import { exec } from 'node:child_process'
13
13
import open from 'open'
14
14
import spawn from 'cross-spawn'
15
15
import colors from 'picocolors'
@@ -18,25 +18,23 @@ import { VITE_PACKAGE_DIR } from '../constants'
18
18
19
19
/**
20
20
* Reads the BROWSER environment variable and decides what to do with it.
21
- * Returns true if it opened a browser or ran a node.js script, otherwise false.
22
21
*/
23
22
export function openBrowser (
24
23
url : string ,
25
24
opt : string | true ,
26
25
logger : Logger ,
27
- ) : boolean {
26
+ ) : void {
28
27
// The browser executable to open.
29
28
// See https://github.com/sindresorhus/open#app for documentation.
30
29
const browser = typeof opt === 'string' ? opt : process . env . BROWSER || ''
31
30
if ( browser . toLowerCase ( ) . endsWith ( '.js' ) ) {
32
- return executeNodeScript ( browser , url , logger )
31
+ executeNodeScript ( browser , url , logger )
33
32
} else if ( browser . toLowerCase ( ) !== 'none' ) {
34
33
const browserArgs = process . env . BROWSER_ARGS
35
34
? process . env . BROWSER_ARGS . split ( ' ' )
36
35
: [ ]
37
- return startBrowserProcess ( browser , browserArgs , url )
36
+ startBrowserProcess ( browser , browserArgs , url )
38
37
}
39
- return false
40
38
}
41
39
42
40
function executeNodeScript ( scriptPath : string , url : string , logger : Logger ) {
@@ -70,7 +68,7 @@ const supportedChromiumBrowsers = [
70
68
'Chromium' ,
71
69
]
72
70
73
- function startBrowserProcess (
71
+ async function startBrowserProcess (
74
72
browser : string | undefined ,
75
73
browserArgs : string [ ] ,
76
74
url : string ,
@@ -88,20 +86,19 @@ function startBrowserProcess(
88
86
89
87
if ( shouldTryOpenChromeWithAppleScript ) {
90
88
try {
91
- const ps = execSync ( 'ps cax' ) . toString ( )
89
+ const ps = await exec ( 'ps cax' ) . toString ( )
92
90
const openedBrowser =
93
91
preferredOSXBrowser && ps . includes ( preferredOSXBrowser )
94
92
? preferredOSXBrowser
95
93
: supportedChromiumBrowsers . find ( ( b ) => ps . includes ( b ) )
96
94
if ( openedBrowser ) {
97
95
// Try our best to reuse existing tab with AppleScript
98
- execSync (
96
+ await exec (
99
97
`osascript openChrome.applescript "${ encodeURI (
100
98
url ,
101
99
) } " "${ openedBrowser } "`,
102
100
{
103
101
cwd : join ( VITE_PACKAGE_DIR , 'bin' ) ,
104
- stdio : 'ignore' ,
105
102
} ,
106
103
)
107
104
return true
0 commit comments