Files
react-native/packages/dev-middleware/src/utils/DefaultBrowserLauncher.js
T
Tim Yung 3e6423fe65 RN: Flowify packages/rn-tester (#51788)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51788

Adds `flow` to the remaining files that are lacking it in the `packages/rn-tester` directory.

This also adds any necessary type annotations and fixes lint warnings.

Changelog:
[Internal]

Reviewed By: SamChou19815

Differential Revision: D75899307

fbshipit-source-id: 27a74ed0007b3b754446a45931c2c148312d5e3a
2025-06-04 12:03:52 -07:00

68 lines
1.7 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
const {spawn} = require('child_process');
const ChromeLauncher = require('chrome-launcher');
const {Launcher: EdgeLauncher} = require('chromium-edge-launcher');
const open = require('open');
/**
* Default `BrowserLauncher` implementation which opens URLs on the host
* machine.
*/
const DefaultBrowserLauncher = {
/**
* Attempt to open the debugger frontend in a Google Chrome or Microsoft Edge
* app window.
*/
launchDebuggerAppWindow: async (url: string): Promise<void> => {
let chromePath;
try {
// Locate Chrome installation path, will throw if not found
chromePath = ChromeLauncher.getChromePath();
} catch (e) {
// Fall back to Microsoft Edge
chromePath = EdgeLauncher.getFirstInstallation();
}
if (chromePath == null) {
// Fall back to default browser - the frontend will warn if the browser
// is not supported.
await open(url);
return;
}
const chromeFlags = [`--app=${url}`, '--window-size=1200,600'];
return new Promise((resolve, reject) => {
const childProcess = spawn(chromePath, chromeFlags, {
detached: true,
stdio: 'ignore',
});
childProcess.on('data', () => {
resolve();
});
childProcess.on('close', (code: number) => {
if (code !== 0) {
reject(
new Error(
`Failed to launch debugger app window: ${chromePath} exited with code ${code}`,
),
);
}
});
});
},
};
export default DefaultBrowserLauncher;