Files
react-native/packages/debugger-shell/src/node/index.flow.js
T
Moti ZilbermanandFacebook GitHub Bot 63f4fb129f Scaffold debugger-shell package (#51688)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51688

Changelog: [Internal]

# Context

See D74904547.

## This diff

Creates the `react-native/debugger-shell` package, containing a basic implementation of an Electron-based shell for React Native DevTools. At this point, there is no direct dependency on the new package from the rest of React Native - it's designed to be used as part of a Meta-internal experimental rollout of the new debugger shell via the `BrowserLauncher` interface in `dev-middleware`.

Reviewed By: huntie

Differential Revision: D74820232

fbshipit-source-id: cb06ea9e2ed8c8822019cad8296cc19e69f9db0b
2025-05-30 02:23:12 -07:00

78 lines
2.2 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('cross-spawn');
async function unstable_spawnDebuggerShellWithArgs(
args: string[],
{
mode = 'detached',
}: $ReadOnly<{
// In 'syncAndExit' mode, the current process will block until the spawned process exits, and then it will exit
// with the same exit code as the spawned process.
// In 'detached' mode, the spawned process will be detached from the current process and the current process will
// continue to run normally.
mode?: 'syncThenExit' | 'detached',
}> = {},
): Promise<void> {
// NOTE: Internally at Meta, this is aliased to a workspace that is
// API-compatible with the 'electron' package, but contains prebuilt binaries
// that do not need to be downloaded in a postinstall action.
const electronPath = require('electron');
return new Promise((resolve, reject) => {
const child = spawn(
electronPath,
[require.resolve('../electron'), ...args],
{
stdio: 'inherit',
windowsHide: true,
detached: mode === 'detached',
},
);
if (mode === 'detached') {
child.on('spawn', () => {
resolve();
});
child.on('close', (code /*: number */) => {
if (code !== 0) {
reject(
new Error(
`Failed to open debugger shell: ${electronPath} exited with code ${code}`,
),
);
}
});
child.unref();
} else if (mode === 'syncThenExit') {
child.on('close', function (code, signal) {
if (code === null) {
console.error(electronPath, 'exited with signal', signal);
process.exit(1);
}
process.exit(code);
});
const handleTerminationSignal = function (signal: string) {
process.on(signal, function signalHandler() {
if (!child.killed) {
child.kill(signal);
}
});
};
handleTerminationSignal('SIGINT');
handleTerminationSignal('SIGTERM');
}
});
}
export {unstable_spawnDebuggerShellWithArgs};