|
| 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/facebook/create-react-app/blob/main/LICENSE |
| 7 | +
|
| 8 | +Modified for use in html-reporter, based on Vite's implementation. |
| 9 | +*/ |
| 10 | + |
| 11 | +/* global Application */ |
| 12 | + |
| 13 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 14 | +function run(argv) { |
| 15 | + const urlToOpen = argv[0]; |
| 16 | + // Allow requested program to be optional, default to Google Chrome |
| 17 | + const programName = argv[1] ?? 'Google Chrome'; |
| 18 | + |
| 19 | + const app = Application(programName); |
| 20 | + |
| 21 | + if (app.windows.length === 0) { |
| 22 | + app.Window().make(); |
| 23 | + } |
| 24 | + |
| 25 | + // 1: Looking for tab running debugger then, |
| 26 | + // Reload debugging tab if found, then return |
| 27 | + const found = lookupTabWithUrl(urlToOpen, app); |
| 28 | + if (found) { |
| 29 | + found.targetWindow.activeTabIndex = found.targetTabIndex; |
| 30 | + found.targetTab.reload(); |
| 31 | + found.targetWindow.index = 1; |
| 32 | + app.activate(); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + // 2: Looking for Empty tab |
| 37 | + // In case debugging tab was not found |
| 38 | + // We try to find an empty tab instead |
| 39 | + const emptyTabFound = lookupTabWithUrl('chrome://newtab/', app); |
| 40 | + if (emptyTabFound) { |
| 41 | + emptyTabFound.targetWindow.activeTabIndex = emptyTabFound.targetTabIndex; |
| 42 | + emptyTabFound.targetTab.url = urlToOpen; |
| 43 | + app.activate(); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // 3: Create new tab |
| 48 | + // both debugging and empty tab were not found make a new tab with url |
| 49 | + const firstWindow = app.windows[0]; |
| 50 | + firstWindow.tabs.push(app.Tab({url: urlToOpen})); |
| 51 | + app.activate(); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Lookup tab with given url |
| 56 | + */ |
| 57 | +function lookupTabWithUrl(lookupUrl, app) { |
| 58 | + const windows = app.windows(); |
| 59 | + for (const window of windows) { |
| 60 | + for (const [tabIndex, tab] of window.tabs().entries()) { |
| 61 | + if (tab.url().includes(lookupUrl)) { |
| 62 | + return { |
| 63 | + targetTab: tab, |
| 64 | + targetTabIndex: tabIndex + 1, |
| 65 | + targetWindow: window |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments