( browser: string | undefined, browserArgs: string[], url: string, logger: Logger, )
| 70 | ] |
| 71 | |
| 72 | async function startBrowserProcess( |
| 73 | browser: string | undefined, |
| 74 | browserArgs: string[], |
| 75 | url: string, |
| 76 | logger: Logger, |
| 77 | ) { |
| 78 | // If we're on OS X, the user hasn't specifically |
| 79 | // requested a different browser, we can try opening |
| 80 | // a Chromium browser with JXA. This lets us reuse an |
| 81 | // existing tab when possible instead of creating a new one. |
| 82 | const preferredOSXBrowser = |
| 83 | browser === 'google chrome' ? 'Google Chrome' : browser |
| 84 | const shouldTryOpenChromeWithJXA = |
| 85 | process.platform === 'darwin' && |
| 86 | (!preferredOSXBrowser || |
| 87 | supportedChromiumBrowsers.includes(preferredOSXBrowser)) |
| 88 | |
| 89 | if (shouldTryOpenChromeWithJXA) { |
| 90 | try { |
| 91 | const ps = await execAsync('ps cax') |
| 92 | const openedBrowser = |
| 93 | preferredOSXBrowser && ps.includes(preferredOSXBrowser) |
| 94 | ? preferredOSXBrowser |
| 95 | : supportedChromiumBrowsers.find((b) => ps.includes(b)) |
| 96 | if (openedBrowser) { |
| 97 | // Try our best to reuse existing tab with JXA |
| 98 | await execAsync(`osascript openChrome.js "${url}" "${openedBrowser}"`, { |
| 99 | cwd: join(VITE_PACKAGE_DIR, 'bin'), |
| 100 | }) |
| 101 | return true |
| 102 | } |
| 103 | } catch { |
| 104 | // Ignore errors |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Another special case: on OS X, check if BROWSER has been set to "open". |
| 109 | // In this case, instead of passing the string `open` to `open` function (which won't work), |
| 110 | // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser): |
| 111 | // https://github.com/react/create-react-app/pull/1690#issuecomment-283518768 |
| 112 | if (process.platform === 'darwin' && browser === 'open') { |
| 113 | browser = undefined |
| 114 | } |
| 115 | |
| 116 | // Fallback to open |
| 117 | // (It will always open new tab) |
| 118 | try { |
| 119 | const options: Options = browser |
| 120 | ? { app: { name: browser, arguments: browserArgs } } |
| 121 | : {} |
| 122 | |
| 123 | new Promise((_, reject) => { |
| 124 | open(url, options) |
| 125 | .then((subprocess) => { |
| 126 | subprocess.on('error', reject) |
| 127 | }) |
| 128 | .catch(reject) |
| 129 | }).catch((err) => { |
no test coverage detected