diff --git a/packages/vite/bin/openChrome.applescript b/packages/vite/bin/openChrome.applescript deleted file mode 100644 index 9ce2293231987a..00000000000000 --- a/packages/vite/bin/openChrome.applescript +++ /dev/null @@ -1,95 +0,0 @@ -(* -Copyright (c) 2015-present, Facebook, Inc. - -This source code is licensed under the MIT license found in the -LICENSE file at -https://github.com/facebookincubator/create-react-app/blob/master/LICENSE -*) - -property targetTab: null -property targetTabIndex: -1 -property targetWindow: null -property theProgram: "Google Chrome" - -on run argv - set theURL to item 1 of argv - - -- Allow requested program to be optional, - -- default to Google Chrome - if (count of argv) > 1 then - set theProgram to item 2 of argv - end if - - using terms from application "Google Chrome" - tell application theProgram - - if (count every window) = 0 then - make new window - end if - - -- 1: Looking for tab running debugger - -- then, Reload debugging tab if found - -- then return - set found to my lookupTabWithUrl(theURL) - if found then - set targetWindow's active tab index to targetTabIndex - tell targetTab to reload - tell targetWindow to activate - set index of targetWindow to 1 - return - end if - - -- 2: Looking for Empty tab - -- In case debugging tab was not found - -- We try to find an empty tab instead - set found to my lookupTabWithUrl("chrome://newtab/") - if found then - set targetWindow's active tab index to targetTabIndex - set URL of targetTab to theURL - tell targetWindow to activate - return - end if - - -- 3: Create new tab - -- both debugging and empty tab were not found - -- make a new tab with url - tell window 1 - activate - make new tab with properties {URL:theURL} - end tell - end tell - end using terms from -end run - --- Function: --- Lookup tab with given url --- if found, store tab, index, and window in properties --- (properties were declared on top of file) -on lookupTabWithUrl(lookupUrl) - using terms from application "Google Chrome" - tell application theProgram - -- Find a tab with the given url - set found to false - set theTabIndex to -1 - repeat with theWindow in every window - set theTabIndex to 0 - repeat with theTab in every tab of theWindow - set theTabIndex to theTabIndex + 1 - if (theTab's URL as string) contains lookupUrl then - -- assign tab, tab index, and window to properties - set targetTab to theTab - set targetTabIndex to theTabIndex - set targetWindow to theWindow - set found to true - exit repeat - end if - end repeat - - if found then - exit repeat - end if - end repeat - end tell - end using terms from - return found -end lookupTabWithUrl diff --git a/packages/vite/bin/openChrome.js b/packages/vite/bin/openChrome.js new file mode 100644 index 00000000000000..f051e2951c9aea --- /dev/null +++ b/packages/vite/bin/openChrome.js @@ -0,0 +1,68 @@ +/* +Copyright (c) 2015-present, Facebook, Inc. + +This source code is licensed under the MIT license found in the +LICENSE file at +https://github.com/facebookincubator/create-react-app/blob/master/LICENSE +*/ + +/* global Application */ + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function run(argv) { + const urlToOpen = argv[0] + // Allow requested program to be optional, default to Google Chrome + const programName = argv[1] ?? 'Google Chrome' + + const app = Application(programName) + + if (app.windows.length === 0) { + app.Window().make() + } + + // 1: Looking for tab running debugger then, + // Reload debugging tab if found, then return + const found = lookupTabWithUrl(urlToOpen, app) + if (found) { + found.targetWindow.activeTabIndex = found.targetTabIndex + found.targetTab.reload() + found.targetWindow.index = 1 + app.activate() + return + } + + // 2: Looking for Empty tab + // In case debugging tab was not found + // We try to find an empty tab instead + const emptyTabFound = lookupTabWithUrl('chrome://newtab/', app) + if (emptyTabFound) { + emptyTabFound.targetWindow.activeTabIndex = emptyTabFound.targetTabIndex + emptyTabFound.targetTab.url = urlToOpen + app.activate() + return + } + + // 3: Create new tab + // both debugging and empty tab were not found make a new tab with url + const firstWindow = app.windows[0] + firstWindow.tabs.push(app.Tab({ url: urlToOpen })) + app.activate() +} + +/** + * Lookup tab with given url + */ +function lookupTabWithUrl(lookupUrl, app) { + const windows = app.windows() + for (const window of windows) { + for (const [tabIndex, tab] of window.tabs().entries()) { + if (tab.url().includes(lookupUrl)) { + return { + targetTab: tab, + targetTabIndex: tabIndex + 1, + targetWindow: window, + } + } + } + } +} diff --git a/packages/vite/src/node/server/openBrowser.ts b/packages/vite/src/node/server/openBrowser.ts index eebf4b0ad350fe..091e2881e3b1c5 100644 --- a/packages/vite/src/node/server/openBrowser.ts +++ b/packages/vite/src/node/server/openBrowser.ts @@ -77,16 +77,16 @@ async function startBrowserProcess( ) { // If we're on OS X, the user hasn't specifically // requested a different browser, we can try opening - // a Chromium browser with AppleScript. This lets us reuse an + // a Chromium browser with JXA. This lets us reuse an // existing tab when possible instead of creating a new one. const preferredOSXBrowser = browser === 'google chrome' ? 'Google Chrome' : browser - const shouldTryOpenChromeWithAppleScript = + const shouldTryOpenChromeWithJXA = process.platform === 'darwin' && (!preferredOSXBrowser || supportedChromiumBrowsers.includes(preferredOSXBrowser)) - if (shouldTryOpenChromeWithAppleScript) { + if (shouldTryOpenChromeWithJXA) { try { const ps = await execAsync('ps cax') const openedBrowser = @@ -94,13 +94,10 @@ async function startBrowserProcess( ? preferredOSXBrowser : supportedChromiumBrowsers.find((b) => ps.includes(b)) if (openedBrowser) { - // Try our best to reuse existing tab with AppleScript - await execAsync( - `osascript openChrome.applescript "${url}" "${openedBrowser}"`, - { - cwd: join(VITE_PACKAGE_DIR, 'bin'), - }, - ) + // Try our best to reuse existing tab with JXA + await execAsync(`osascript openChrome.js "${url}" "${openedBrowser}"`, { + cwd: join(VITE_PACKAGE_DIR, 'bin'), + }) return true } } catch {