Skip to content

Commit

Permalink
fix: open browser reuse logic (#12535)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 22, 2023
1 parent debc6e2 commit 04d14af
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/vite/src/node/server/openBrowser.ts
Expand Up @@ -10,6 +10,7 @@

import { join } from 'node:path'
import { exec } from 'node:child_process'
import type { ExecOptions } from 'node:child_process'
import open from 'open'
import spawn from 'cross-spawn'
import colors from 'picocolors'
Expand Down Expand Up @@ -54,7 +55,6 @@ function executeNodeScript(scriptPath: string, url: string, logger: Logger) {
)
}
})
return true
}

const supportedChromiumBrowsers = [
Expand Down Expand Up @@ -86,14 +86,14 @@ async function startBrowserProcess(

if (shouldTryOpenChromeWithAppleScript) {
try {
const ps = await exec('ps cax').toString()
const ps = await execAsync('ps cax')
const openedBrowser =
preferredOSXBrowser && ps.includes(preferredOSXBrowser)
? preferredOSXBrowser
: supportedChromiumBrowsers.find((b) => ps.includes(b))
if (openedBrowser) {
// Try our best to reuse existing tab with AppleScript
await exec(
await execAsync(
`osascript openChrome.applescript "${encodeURI(
url,
)}" "${openedBrowser}"`,
Expand Down Expand Up @@ -128,3 +128,15 @@ async function startBrowserProcess(
return false
}
}

function execAsync(command: string, options?: ExecOptions): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, options, (error, stdout) => {
if (error) {
reject(error)
} else {
resolve(stdout.toString())
}
})
})
}

0 comments on commit 04d14af

Please sign in to comment.