Skip to content

Commit 7979f9d

Browse files
authored
refactor: rewrite openchrome.applescript to JXA (vitejs#20424)
1 parent 8033e5b commit 7979f9d

File tree

3 files changed

+75
-105
lines changed

3 files changed

+75
-105
lines changed

packages/vite/bin/openChrome.applescript

Lines changed: 0 additions & 95 deletions
This file was deleted.

packages/vite/bin/openChrome.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright (c) 2015-present, Facebook, Inc.
3+
4+
This source code is licensed under the MIT license found in the
5+
LICENSE file at
6+
https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
7+
*/
8+
9+
/* global Application */
10+
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
function run(argv) {
13+
const urlToOpen = argv[0]
14+
// Allow requested program to be optional, default to Google Chrome
15+
const programName = argv[1] ?? 'Google Chrome'
16+
17+
const app = Application(programName)
18+
19+
if (app.windows.length === 0) {
20+
app.Window().make()
21+
}
22+
23+
// 1: Looking for tab running debugger then,
24+
// Reload debugging tab if found, then return
25+
const found = lookupTabWithUrl(urlToOpen, app)
26+
if (found) {
27+
found.targetWindow.activeTabIndex = found.targetTabIndex
28+
found.targetTab.reload()
29+
found.targetWindow.index = 1
30+
app.activate()
31+
return
32+
}
33+
34+
// 2: Looking for Empty tab
35+
// In case debugging tab was not found
36+
// We try to find an empty tab instead
37+
const emptyTabFound = lookupTabWithUrl('chrome://newtab/', app)
38+
if (emptyTabFound) {
39+
emptyTabFound.targetWindow.activeTabIndex = emptyTabFound.targetTabIndex
40+
emptyTabFound.targetTab.url = urlToOpen
41+
app.activate()
42+
return
43+
}
44+
45+
// 3: Create new tab
46+
// both debugging and empty tab were not found make a new tab with url
47+
const firstWindow = app.windows[0]
48+
firstWindow.tabs.push(app.Tab({ url: urlToOpen }))
49+
app.activate()
50+
}
51+
52+
/**
53+
* Lookup tab with given url
54+
*/
55+
function lookupTabWithUrl(lookupUrl, app) {
56+
const windows = app.windows()
57+
for (const window of windows) {
58+
for (const [tabIndex, tab] of window.tabs().entries()) {
59+
if (tab.url().includes(lookupUrl)) {
60+
return {
61+
targetTab: tab,
62+
targetTabIndex: tabIndex + 1,
63+
targetWindow: window,
64+
}
65+
}
66+
}
67+
}
68+
}

packages/vite/src/node/server/openBrowser.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,27 @@ async function startBrowserProcess(
7777
) {
7878
// If we're on OS X, the user hasn't specifically
7979
// requested a different browser, we can try opening
80-
// a Chromium browser with AppleScript. This lets us reuse an
80+
// a Chromium browser with JXA. This lets us reuse an
8181
// existing tab when possible instead of creating a new one.
8282
const preferredOSXBrowser =
8383
browser === 'google chrome' ? 'Google Chrome' : browser
84-
const shouldTryOpenChromeWithAppleScript =
84+
const shouldTryOpenChromeWithJXA =
8585
process.platform === 'darwin' &&
8686
(!preferredOSXBrowser ||
8787
supportedChromiumBrowsers.includes(preferredOSXBrowser))
8888

89-
if (shouldTryOpenChromeWithAppleScript) {
89+
if (shouldTryOpenChromeWithJXA) {
9090
try {
9191
const ps = await execAsync('ps cax')
9292
const openedBrowser =
9393
preferredOSXBrowser && ps.includes(preferredOSXBrowser)
9494
? preferredOSXBrowser
9595
: supportedChromiumBrowsers.find((b) => ps.includes(b))
9696
if (openedBrowser) {
97-
// Try our best to reuse existing tab with AppleScript
98-
await execAsync(
99-
`osascript openChrome.applescript "${url}" "${openedBrowser}"`,
100-
{
101-
cwd: join(VITE_PACKAGE_DIR, 'bin'),
102-
},
103-
)
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+
})
104101
return true
105102
}
106103
} catch {

0 commit comments

Comments
 (0)