|
| 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 | +} |
0 commit comments