Skip to content

Commit 4dfaa39

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Relocate CLI plugin server modules (#57654)
Summary: Pull Request resolved: #57654 Reorganise these modules under a `src/dev-server/` directory (out of `src/(commands|utils)/`, more clearly identifying this unit of the codebase. No public API change: the package still exports `startCommand` unchanged. Renames inside this directory: - `middleware.js` → `loadCommunityMiddleware.js` - `runServer.js` → `runDevServer.js` Changelog: [Internal] Differential Revision: D113412985
1 parent 4b38ade commit 4dfaa39

9 files changed

Lines changed: 108 additions & 110 deletions

File tree

packages/community-cli-plugin/src/commands/start/index.js renamed to packages/community-cli-plugin/src/commands/start.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010

1111
import type {Command} from '@react-native-community/cli-types';
1212

13-
import runServer from './runServer';
13+
import runDevServer from '../dev-server/runDevServer';
1414
import path from 'node:path';
1515

16-
export type {StartCommandArgs} from './runServer';
17-
1816
const startCommand: Command = {
1917
name: 'start',
20-
func: runServer,
18+
func: runDevServer,
2119
description: 'Start the React Native development server.',
2220
options: [
2321
{

packages/community-cli-plugin/src/commands/start/middleware.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

packages/community-cli-plugin/src/commands/start/OpenDebuggerKeyboardHandler.js renamed to packages/community-cli-plugin/src/dev-server/OpenDebuggerKeyboardHandler.js

File renamed without changes.

packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js renamed to packages/community-cli-plugin/src/dev-server/attachKeyHandlers.js

File renamed without changes.

packages/community-cli-plugin/src/utils/createDevMiddlewareLogger.js renamed to packages/community-cli-plugin/src/dev-server/createDevMiddlewareLogger.js

File renamed without changes.

packages/community-cli-plugin/src/utils/isDevServerRunning.js renamed to packages/community-cli-plugin/src/dev-server/isDevServerRunning.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default async function isDevServerRunning(
3333
return 'not_running';
3434
}
3535

36+
// FIXME: Depends on @react-native-community/cli-server-api
3637
const statusResponse = await fetch(`${devServerUrl}/status`);
3738
const body = await statusResponse.text();
3839

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {Server} from 'connect';
12+
import type {TerminalReportableEvent} from 'metro';
13+
14+
const debug = require('debug')('ReactNative:CommunityCliPlugin');
15+
16+
type DevServerMiddlewareFactory = (params: {
17+
host?: string,
18+
port: number,
19+
watchFolders: ReadonlyArray<string>,
20+
}) => {
21+
middleware: Server,
22+
websocketEndpoints: {[path: string]: ws$WebSocketServer},
23+
messageSocketEndpoint: {
24+
server: ws$WebSocketServer,
25+
broadcast: (
26+
method: string,
27+
params?: Record<string, unknown> | null,
28+
) => void,
29+
},
30+
eventsSocketEndpoint: {
31+
server: ws$WebSocketServer,
32+
reportEvent: (event: TerminalReportableEvent) => void,
33+
},
34+
...
35+
};
36+
37+
// $FlowFixMe[incompatible-type]
38+
const unusedStubWSServer: ws$WebSocketServer = {};
39+
// $FlowFixMe[incompatible-type]
40+
const unusedMiddlewareStub: Server = {};
41+
42+
// FIXME: Several features will break without community middleware
43+
// (@react-native-community/cli-server-api) and should be migrated into core.
44+
// e.g. used by packages/react-native/Libraries/Core/Devtools/:
45+
// - /open-stack-frame
46+
// - /open-url
47+
// - /symbolicate
48+
// e.g. used by ./isDevServerRunning.js:
49+
// - /status
50+
const communityMiddlewareFallback: DevServerMiddlewareFactory = () => ({
51+
middleware: unusedMiddlewareStub,
52+
websocketEndpoints: {},
53+
messageSocketEndpoint: {
54+
server: unusedStubWSServer,
55+
broadcast: (
56+
method: string,
57+
_params?: Record<string, unknown> | null,
58+
): void => {},
59+
},
60+
eventsSocketEndpoint: {
61+
server: unusedStubWSServer,
62+
reportEvent: (event: TerminalReportableEvent) => {},
63+
},
64+
});
65+
66+
/**
67+
* Attempt to load the `createDevServerMiddleware` factory from
68+
* `@react-native-community/cli` (an optional peer dependency). If it cannot be
69+
* found, return a factory that produces stub middleware instead.
70+
*/
71+
export default function loadCommunityMiddleware(): DevServerMiddlewareFactory {
72+
try {
73+
// `@react-native-community/cli` is an optional peer dependency of this
74+
// package, and should be a dev dependency of the host project (via the
75+
// community template's package.json).
76+
const communityCliPath = require.resolve('@react-native-community/cli');
77+
78+
// Until https://github.com/react-native-community/cli/pull/2605 lands,
79+
// we need to find `@react-native-community/cli-server-api` via
80+
// `@react-native-community/cli`. Once that lands, we can simply
81+
// require('@react-native-community/cli').
82+
const communityCliServerApiPath = require.resolve(
83+
'@react-native-community/cli-server-api',
84+
{paths: [communityCliPath]},
85+
);
86+
// $FlowFixMe[unsupported-syntax] dynamic import
87+
return require(communityCliServerApiPath)
88+
.createDevServerMiddleware as CreateDevServerMiddleware;
89+
} catch {
90+
debug(`⚠️ Unable to find @react-native-community/cli-server-api
91+
Starting the server without the community middleware.`);
92+
return communityMiddlewareFallback;
93+
}
94+
}

packages/community-cli-plugin/src/commands/start/runServer.js renamed to packages/community-cli-plugin/src/dev-server/runDevServer.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
import type {Config} from '@react-native-community/cli-types';
1212
import type {Reporter, TerminalReportableEvent, TerminalReporter} from 'metro';
1313

14-
import createDevMiddlewareLogger from '../../utils/createDevMiddlewareLogger';
15-
import isDevServerRunning from '../../utils/isDevServerRunning';
16-
import loadMetroConfig from '../../utils/loadMetroConfig';
17-
import * as version from '../../utils/version';
14+
import loadMetroConfig from '../utils/loadMetroConfig';
1815
import attachKeyHandlers from './attachKeyHandlers';
19-
import {createDevServerMiddleware} from './middleware';
16+
import createDevMiddlewareLogger from './createDevMiddlewareLogger';
17+
import isDevServerRunning from './isDevServerRunning';
18+
import loadCommunityMiddleware from './loadCommunityMiddleware';
19+
import * as version from './version';
2020
import {createDevMiddleware} from '@react-native/dev-middleware';
2121
import * as Metro from 'metro';
2222
import path from 'node:path';
2323
import url from 'node:url';
2424
import {styleText} from 'node:util';
2525

26-
export type StartCommandArgs = {
26+
export type DevServerOptions = {
2727
assetPlugins?: string[],
2828
cert?: string,
2929
customLogReporterPath?: string,
@@ -43,10 +43,10 @@ export type StartCommandArgs = {
4343
clientLogs: boolean,
4444
};
4545

46-
async function runServer(
46+
async function runDevServer(
4747
_argv: Array<string>,
4848
cliConfig: Config,
49-
args: StartCommandArgs,
49+
args: DevServerOptions,
5050
) {
5151
const metroConfig = await loadMetroConfig(cliConfig, {
5252
config: args.config,
@@ -107,12 +107,13 @@ async function runServer(
107107
const ReporterImpl = getReporterImpl(args.customLogReporterPath);
108108
const terminalReporter = new ReporterImpl(terminal);
109109

110+
const createCommunityMiddleware = loadCommunityMiddleware();
110111
const {
111112
middleware: communityMiddleware,
112113
websocketEndpoints: communityWebsocketEndpoints,
113114
messageSocketEndpoint,
114115
eventsSocketEndpoint,
115-
} = createDevServerMiddleware({
116+
} = createCommunityMiddleware({
116117
host: hostname,
117118
port,
118119
watchFolders,
@@ -186,4 +187,4 @@ function getReporterImpl(
186187
}
187188
}
188189

189-
export default runServer;
190+
export default runDevServer;
File renamed without changes.

0 commit comments

Comments
 (0)