Move codegen and spm command wrappers into community-cli-plugin (#57652)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57652

Migrate the `codegen` and `spm` command wrappers (Community CLI specific) out of `react-native.config.js` and into the `react-native/community-cli-plugin` package, alongside the existing `bundleCommand` and `startCommand`.

This continues moving command definitions into the plugin so `react-native.config.js` becomes a thin routing layer, as the file's own comment already calls for.

**Changes**

- Add `src/commands/spm/index.js` and `src/commands/codegen/index.js`, each defining and default-exporting its command (unchanged `name`, `description`, and `options`) plus an exported args type — mirroring the `bundle`/`start` command structure.
- Export `spmCommand` and `codegenCommand` from `src/index.flow.js`.
- Drop the inline `spmCommand` and `codegenCommand` definitions from `react-native.config.js`.
- Add docs to README.

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D113412987

fbshipit-source-id: a39a7ef208ee970f2b7f9d4c38a86c39752db09a
This commit is contained in:
Alex Hunt authored and meta-codesync[bot] committed 2026-07-25 10:32:46 -07:00
1 parent f46ace878a
commit cec7c453fb
5 files changed
+235 -122

No files matched your search

+42
View File
@@ -75,6 +75,48 @@ npx @react-native-community/cli bundle --entry-file <path> [options]
| `--read-global-cache` | Attempt to fetch transformed JS code from the global cache, if configured. Defaults to `false`. |
| `--config <string>` | Path to the CLI configuration file. |
### `codegen`
Run the React Native codegen, generating native boilerplate from JS spec files.
#### Usage
```sh
npx @react-native-community/cli codegen [options]
```
#### Options
| Option | Description |
| - | - |
| `--path <path>` | Path to the React Native project root. Defaults to the current working directory. |
| `--platform <string>` | Target platform. Supported values: `"android"`, `"ios"`, `"all"`. Defaults to `"all"`. |
| `--outputPath <path>` | Path where generated artifacts will be output to. |
| `--source <string>` | Whether the script is invoked from an `app` or a `library`. Defaults to `"app"`. |
### `spm [action]`
Set up or maintain Swift Package Manager support for the iOS/macOS app. Actions: `add`, `update`, `deinit`, `scaffold`. With no action: `add` (or `update` if SPM is already set up).
#### Usage
```sh
npx @react-native-community/cli spm [action] [options]
```
#### Options
| Option | Description |
| - | - |
| `--version <string>` | React Native version (e.g. `0.80.0`). Defaults to the version in `node_modules/react-native/package.json`. |
| `--yes` | Skip the dirty-pbxproj confirmation prompt. |
| `--xcodeproj <path>` | **[add]** Path to the `.xcodeproj` to inject SPM packages into (disambiguates when several exist). |
| `--productName <string>` | **[add]** App target to inject into (disambiguates when several exist). |
| `--deintegrate` | **[add]** Run `pod deintegrate` and strip React Native from the Podfile before injecting (CocoaPods → SwiftPM migration). |
| `--artifacts <path>` | **[advanced]** Local artifact root containing complete `debug/` and `release/` slots. |
| `--download <string>` | **[advanced]** Artifact download policy: `auto` (default), `skip`, or `force`. |
| `--skipCodegen` | **[advanced]** Skip the react-native codegen step. |
## Contributing
Changes to this package can be made locally and tested against the `rn-tester` app, per the [Contributing guide](https://reactnative.dev/contributing/overview#contributing-code). During development, this package is automatically run from source with no build step.
@@ -0,0 +1,63 @@
/**
* 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
*/
import type {Command, Config} from '@react-native-community/cli-types';
export type CodegenCommandArgs = {
path: string,
platform: string,
outputPath?: string,
source: string,
};
const codegenCommand: Command = {
name: 'codegen',
options: [
{
name: '--path <path>',
description: 'Path to the React Native project root.',
default: process.cwd(),
},
{
name: '--platform <string>',
description:
'Target platform. Supported values: "android", "ios", "all".',
default: 'all',
},
{
name: '--outputPath <path>',
description: 'Path where generated artifacts will be output to.',
},
{
name: '--source <string>',
description: 'Whether the script is invoked from an `app` or a `library`',
default: 'app',
},
],
func: (
argv: Array<string>,
config: Config,
args: CodegenCommandArgs,
): void => {
const generateArtifactsExecutor = require.resolve(
'react-native/scripts/codegen/generate-artifacts-executor',
{paths: [config.root]},
);
// $FlowFixMe[unsupported-syntax] dynamic require of a resolved path
require(generateArtifactsExecutor).execute(
args.path,
args.platform,
args.outputPath,
args.source,
);
},
};
export default codegenCommand;
@@ -0,0 +1,118 @@
/**
* 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
*/
import type {Command, Config} from '@react-native-community/cli-types';
export type SpmCommandArgs = {
version?: string,
yes?: boolean,
xcodeproj?: string,
productName?: string,
deintegrate?: boolean,
artifacts?: string,
download?: string,
skipCodegen?: boolean,
};
const spmCommand: Command = {
name: 'spm [action]',
description:
'Set up or maintain Swift Package Manager support for the iOS/macOS app. ' +
'Actions: add, update, deinit, scaffold. With no action: add (or update ' +
'if SPM is already set up).',
options: [
{
name: '--version <string>',
description:
'React Native version (e.g. 0.80.0). Defaults to the version in node_modules/react-native/package.json.',
},
{
name: '--yes',
description: 'Skip the dirty-pbxproj confirmation prompt.',
},
{
name: '--xcodeproj <path>',
description:
'[add] Path to the .xcodeproj to inject SPM packages into ' +
'(disambiguates when several exist).',
},
{
name: '--productName <string>',
description:
'[add] App target to inject into (disambiguates when several exist).',
},
{
name: '--deintegrate',
description:
'[add] Run `pod deintegrate` and strip React Native from the Podfile ' +
'before injecting (CocoaPods → SwiftPM migration).',
},
{
name: '--artifacts <path>',
description:
'[advanced] Local artifact root containing complete debug/ and release/ slots.',
},
{
name: '--download <string>',
description:
'[advanced] Artifact download policy: auto (default), skip, or force.',
},
{
name: '--skipCodegen',
description: '[advanced] Skip the react-native codegen step.',
},
],
func: async (
argv: Array<string>,
config: Config,
args: SpmCommandArgs,
): Promise<void> => {
const passthrough: Array<string> = [];
if (argv[0] != null) {
passthrough.push(argv[0]);
}
const stringOpts: Array<
[
'version' | 'productName' | 'xcodeproj' | 'artifacts' | 'download',
string,
],
> = [
['version', '--version'],
['productName', '--product-name'],
['xcodeproj', '--xcodeproj'],
['artifacts', '--artifacts'],
['download', '--download'],
];
for (const [key, flag] of stringOpts) {
const value = args[key];
if (value != null) {
passthrough.push(flag, String(value));
}
}
const boolOpts: Array<['skipCodegen' | 'deintegrate' | 'yes', string]> = [
['skipCodegen', '--skip-codegen'],
['deintegrate', '--deintegrate'],
['yes', '--yes'],
];
for (const [key, flag] of boolOpts) {
if (args[key] === true) {
passthrough.push(flag);
}
}
const setupAppleSpm = require.resolve(
'react-native/scripts/setup-apple-spm',
{paths: [config.root]},
);
// $FlowFixMe[unsupported-syntax] dynamic require of a resolved path
await require(setupAppleSpm).main(passthrough);
},
};
export default spmCommand;
@@ -12,6 +12,8 @@ export {
default as bundleCommand,
unstable_createBundleCommandParser,
} from './commands/bundle';
export {default as codegenCommand} from './commands/codegen';
export {default as spmCommand} from './commands/spm';
export {default as startCommand} from './commands/start';
export {unstable_buildBundleWithConfig} from './commands/bundle/buildBundle';
+10 -122
View File
@@ -14,14 +14,16 @@
import type {Command} from '@react-native-community/cli-types';
*/
// React Native shouldn't be exporting itself like this, the Community Template should be be directly
// depending on and injecting:
// IMPORTANT: This is a routing file only. Do NOT add new command
// definitions or implementations here.
//
// New CLI commands belong in @react-native/community-cli-plugin, and
// may (temporarily) be imported and registered here.
//
// Future state: The Community Template should directly depend on and inject:
// - @react-native-community/cli-platform-android
// - @react-native-community/cli-platform-ios
// - @react-native/community-cli-plugin
// - codegen command should be inhoused into @react-native-community/cli
//
// This is a temporary workaround.
const verbose = Boolean(process.env.DEBUG?.includes('react-native'));
@@ -72,126 +74,12 @@ const commands /*: Array<Command> */ = [];
const {
bundleCommand,
codegenCommand,
spmCommand,
startCommand,
} = require('@react-native/community-cli-plugin');
commands.push(bundleCommand, startCommand);
const codegenCommand /*: Command */ = {
name: 'codegen',
options: [
{
name: '--path <path>',
description: 'Path to the React Native project root.',
default: process.cwd(),
},
{
name: '--platform <string>',
description:
'Target platform. Supported values: "android", "ios", "all".',
default: 'all',
},
{
name: '--outputPath <path>',
description: 'Path where generated artifacts will be output to.',
},
{
name: '--source <string>',
description: 'Whether the script is invoked from an `app` or a `library`',
default: 'app',
},
],
func: (argv, config, args) =>
require('./scripts/codegen/generate-artifacts-executor').execute(
args.path,
args.platform,
args.outputPath,
args.source,
),
};
commands.push(codegenCommand);
const spmCommand /*: Command */ = {
name: 'spm [action]',
description:
'Set up or maintain Swift Package Manager support for the iOS/macOS app. ' +
'Actions: add, update, deinit, scaffold. With no action: add (or update ' +
'if SPM is already set up).',
options: [
{
name: '--version <string>',
description:
'React Native version (e.g. 0.80.0). Defaults to the version in node_modules/react-native/package.json.',
},
{
name: '--yes',
description: 'Skip the dirty-pbxproj confirmation prompt.',
},
{
name: '--xcodeproj <path>',
description:
'[add] Path to the .xcodeproj to inject SPM packages into ' +
'(disambiguates when several exist).',
},
{
name: '--productName <string>',
description:
'[add] App target to inject into (disambiguates when several exist).',
},
{
name: '--deintegrate',
description:
'[add] Run `pod deintegrate` and strip React Native from the Podfile ' +
'before injecting (CocoaPods → SwiftPM migration).',
},
{
name: '--artifacts <path>',
description:
'[advanced] Local artifact root containing complete debug/ and release/ slots.',
},
{
name: '--download <string>',
description:
'[advanced] Artifact download policy: auto (default), skip, or force.',
},
{
name: '--skipCodegen',
description: '[advanced] Skip the react-native codegen step.',
},
],
func: async (argv, _config, args) => {
const passthrough /*: Array<string> */ = [];
if (argv[0] != null) {
passthrough.push(argv[0]);
}
const stringOpts /*: Array<[string, string]> */ = [
['version', '--version'],
['productName', '--product-name'],
['xcodeproj', '--xcodeproj'],
['artifacts', '--artifacts'],
['download', '--download'],
];
for (const [key, flag] of stringOpts) {
if (args[key] != null) {
passthrough.push(flag, String(args[key]));
}
}
const boolOpts /*: Array<[string, string]> */ = [
['skipCodegen', '--skip-codegen'],
['deintegrate', '--deintegrate'],
['yes', '--yes'],
];
for (const [key, flag] of boolOpts) {
if (args[key]) {
passthrough.push(flag);
}
}
await require('./scripts/setup-apple-spm').main(passthrough);
},
};
commands.push(spmCommand);
commands.push(bundleCommand, startCommand, spmCommand, codegenCommand);
const config = {
commands,