Add extra parameter to define whether codegen is invoked by lib or app (#48995)

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

This change adds an extra parameter to the codegen script that allow our users to trigger codegen for Apps or for Libraries.

When running codegen for Apps, we have to generate some extra files that are not needed by the Libraries. This is causing issues to our library maintainers and this change will provide more flexibility in the DevX of libraries.

The default value is App, so if the new parameter is not passed, nothing will change in the current behavior.
[iOS][Added] - Add the `source` parameter to generate-codegen-artifacts to avoid generating files not needed by libraries.

Reviewed By: cortinico

Differential Revision: D68765478

fbshipit-source-id: 8030b4472ad4f5058e58b1c91089de5122a4f60a
This commit is contained in:
Riccardo Cipolleschi
2025-01-28 05:06:54 -08:00
committed by Rob Hogan
parent 1f002f9999
commit 4e5da2ea1d
3 changed files with 20 additions and 5 deletions
@@ -68,12 +68,18 @@ const codegenCommand = {
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,
),
};
@@ -899,11 +899,12 @@ function generateFBReactNativeSpecIOS(projectRoot /*: string */) /*: void*/ {
* @parameter projectRoot: the directory with the app source code, where the package.json lives.
* @parameter baseOutputPath: the base output path for the CodeGen.
* @parameter targetPlatform: the target platform. Supported values: 'android', 'ios', 'all'.
* @parameter source: the source that is invoking codegen. Supported values: 'app', 'library'.
* @throws If it can't find a config file for react-native.
* @throws If it can't find a CodeGen configuration in the file.
* @throws If it can't find a cli for the CodeGen.
*/
function execute(projectRoot, targetPlatform, baseOutputPath) {
function execute(projectRoot, targetPlatform, baseOutputPath, source) {
try {
codegenLog(`Analyzing ${path.join(projectRoot, 'package.json')}`);
@@ -951,9 +952,12 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {
platform,
);
generateRCTThirdPartyComponents(libraries, outputPath);
generateCustomURLHandlers(libraries, outputPath);
generateAppDependencyProvider(outputPath);
if (source === 'app') {
// These components are only required by apps, not by libraries
generateRCTThirdPartyComponents(libraries, outputPath);
generateCustomURLHandlers(libraries, outputPath);
generateAppDependencyProvider(outputPath);
}
cleanupEmptyFilesAndFolders(outputPath);
}
@@ -25,7 +25,12 @@ const argv = yargs
alias: 'outputPath',
description: 'Path where generated artifacts will be output to.',
})
.option('s', {
alias: 'source',
description: 'Whether the script is invoked from an `app` or a `library`',
default: 'app',
})
.usage('Usage: $0 -p [path to app] -t [target platform] -o [output path]')
.demandOption(['p', 't']).argv;
executor.execute(argv.path, argv.targetPlatform, argv.outputPath);
executor.execute(argv.path, argv.targetPlatform, argv.outputPath, argv.source);