Skip to content

Commit

Permalink
Sync latest cli-plugin-metro changes
Browse files Browse the repository at this point in the history
Summary:
Update `cli-commands` package to reflect latest source `react-native-community/cli-plugin-metro` changes.

WARNING: This PR is non-functional until the next CLI alpha is published and bumped in `package.json` — since it depends on corresponding new APIs in `react-native-community/cli-tools` (react-native-community/cli#2021). This package (and the upcoming work which integrates it) has been tested against locally linked copies of latest CLI.

- react-native-community/cli#2021
- react-native-community/cli#2024
- react-native-community/cli#2043
- react-native-community/cli#2047

### To do

- [ ] Bump CLI dependencies when next alpha published.
- [ ] Ensure Metro bump from `0.77.0` to `0.78.0` is consistently applied with this.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D48066179

fbshipit-source-id: 964902e7b1bdd9a5ca16768d6b85ef6d9018b44c
  • Loading branch information
huntie authored and facebook-github-bot committed Aug 11, 2023
1 parent 8fa584f commit 02e97ae
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 188 deletions.
33 changes: 33 additions & 0 deletions flow-typed/npm/@react-native-community/cli-tools_v12.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,42 @@
*/

declare module '@react-native-community/cli-tools' {
declare export function addInteractionListener(
callback: (options: {pause: boolean, canEscape?: boolean}) => void,
): void;

declare export function askForPortChange(
port: number,
nextPort: number,
): Promise<{change: boolean}>;

declare export class CLIError extends Error {
constructor(msg: string, originalError?: Error | mixed | string): this;
}

declare export function getNextPort(
port: number,
root: string,
): Promise<{
start: boolean,
nextPort: number,
}>;

declare export function getPidFromPort(port: number): number | null;

declare export function hookStdout(callback: Function): () => void;

declare export function isPackagerRunning(
packagerPort: string | number | void,
): Promise<
| {
status: 'running',
root: string,
}
| 'not_running'
| 'unrecognized',
>;

declare export const logger: $ReadOnly<{
success: (...message: Array<string>) => void,
info: (...message: Array<string>) => void,
Expand All @@ -29,6 +59,9 @@ declare module '@react-native-community/cli-tools' {
enable: () => void,
}>;

declare export function logAlreadyRunningBundler(port: number): void;
declare export function logChangePortInstructions(port: number): void;

declare export function resolveNodeModuleDir(
root: string,
packageName: string,
Expand Down
7 changes: 0 additions & 7 deletions packages/community-cli-plugin/launchPackager.bat

This file was deleted.

10 changes: 0 additions & 10 deletions packages/community-cli-plugin/launchPackager.command

This file was deleted.

14 changes: 7 additions & 7 deletions packages/community-cli-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
"./package.json": "./package.json"
},
"files": [
"dist",
"launchPackager.bat",
"launchPackager.command"
"dist"
],
"dependencies": {
"@react-native-community/cli-server-api": "12.0.0-alpha.7",
"@react-native-community/cli-tools": "12.0.0-alpha.7",
"@react-native/metro-babel-transformer": "^0.73.11",
"chalk": "^4.0.0",
"execa": "^5.1.1",
"metro": "0.77.0",
"metro-config": "0.77.0",
"metro-core": "0.77.0",
"metro-resolver": "0.77.0",
"metro": "0.78.0",
"metro-config": "0.78.0",
"metro-core": "0.78.0",
"readline": "^1.3.0"
},
"devDependencies": {
"metro-resolver": "0.78.0"
},
"engines": {
"node": ">=18"
}
Expand Down
2 changes: 0 additions & 2 deletions packages/community-cli-plugin/src/commands/start/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,3 @@ export default {
},
],
};

export {startServerInNewWindow} from './startServerInNewWindow';
47 changes: 45 additions & 2 deletions packages/community-cli-plugin/src/commands/start/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ import typeof TerminalReporter from 'metro/src/lib/TerminalReporter';
import type Server from 'metro/src/Server';
import type {Middleware} from 'metro-config';

import chalk from 'chalk';
import Metro from 'metro';
import {Terminal} from 'metro-core';
import path from 'path';
import {
createDevServerMiddleware,
indexPageMiddleware,
} from '@react-native-community/cli-server-api';
import {
isPackagerRunning,
logger,
version,
getNextPort,
askForPortChange,
logAlreadyRunningBundler,
logChangePortInstructions,
} from '@react-native-community/cli-tools';

import loadMetroConfig from '../../utils/loadMetroConfig';
import {version} from '@react-native-community/cli-tools';
import enableWatchMode from './watchMode';

export type Args = {
Expand All @@ -48,6 +57,39 @@ export type Args = {
};

async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
let port = args.port ?? 8081;
const packagerStatus = await isPackagerRunning(port);

const handlePortUnavailable = async () => {
const {nextPort, start} = await getNextPort(port, ctx.root);
if (!start) {
logAlreadyRunningBundler(nextPort);
process.exit();
} else {
const {change} = await askForPortChange(port, nextPort);
if (change) {
port = nextPort;
} else {
logChangePortInstructions(port);
process.exit();
}
}
};

if (
typeof packagerStatus === 'object' &&
packagerStatus.status === 'running'
) {
if (packagerStatus.root === ctx.root) {
logAlreadyRunningBundler(port);
process.exit();
} else {
await handlePortUnavailable();
}
} else if (packagerStatus === 'unrecognized') {
await handlePortUnavailable();
}

let reportEvent: (event: any) => void;
const terminal = new Terminal(process.stdout);
const ReporterImpl = getReporterImpl(args.customLogReporterPath);
Expand All @@ -64,7 +106,7 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
const metroConfig = await loadMetroConfig(ctx, {
config: args.config,
maxWorkers: args.maxWorkers,
port: args.port,
port: port,
resetCache: args.resetCache,
watchFolders: args.watchFolders,
projectRoot: args.projectRoot,
Expand Down Expand Up @@ -131,6 +173,7 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
serverInstance.keepAliveTimeout = 30000;

await version.logIfUpdateAvailable(ctx.root);
logger.info(`Started dev server at ${chalk.bold(String(port))}`);
}

function getReporterImpl(customLogReporterPath?: string): TerminalReporter {
Expand Down

This file was deleted.

Loading

0 comments on commit 02e97ae

Please sign in to comment.