Skip to content

Commit 7724e47

Browse files
authored
fix: pass source flag to codegen cli when possible (#815)
### Summary facebook/react-native@98b8f17 adds support for `--source` flag in `npx @react-native-community/cli codegen`. With this flag, we can opt out of our patch. With this PR, bob checks if this flag is supported and uses it if it is. ### Test plan 1. Generate a library with a React Native version that supports `--source`. (The baseline is `[email protected]`). 2. Run `yarn prepare` 3. Make sure the command runs with no errors 4. Check the `ios/generated` and make sure there are no `.podspec` files.
1 parent 5c07d72 commit 7724e47

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

Diff for: packages/create-react-native-library/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { createInitialGitCommit } from './utils/initialCommit';
2020
import { prompt } from './utils/prompt';
2121
import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion';
2222

23-
const FALLBACK_BOB_VERSION = '0.40.4';
23+
const FALLBACK_BOB_VERSION = '0.40.5';
2424
const FALLBACK_NITRO_MODULES_VERSION = '0.22.1';
2525
const SUPPORTED_REACT_NATIVE_VERSION = '0.78.2';
2626

Diff for: packages/react-native-builder-bob/src/targets/codegen/index.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { patchCodegenAndroidPackage } from './patches/patchCodegenAndroidPackage
44
import fs from 'fs-extra';
55
import path from 'path';
66
import del from 'del';
7-
import { removeCodegenAppLevelCode } from './patches/removeCodegenAppLevelCode';
7+
import {
8+
getCodegenCLISourceSupport,
9+
removeCodegenAppLevelCode,
10+
} from './patches/removeCodegenAppLevelCode';
811
import { spawn } from '../../utils/spawn';
912

1013
type Options = Omit<Input, 'output'>;
@@ -42,12 +45,21 @@ export default async function build({ root, report }: Options) {
4245
}
4346

4447
try {
45-
await spawn('npx', ['@react-native-community/cli', 'codegen']);
48+
const codegenCLISupportsSource = await getCodegenCLISourceSupport();
49+
50+
await spawn('npx', [
51+
'@react-native-community/cli',
52+
'codegen',
53+
...(codegenCLISupportsSource ? ['--source', 'library'] : []),
54+
]);
4655

4756
if (codegenType === 'modules' || codegenType === 'all') {
4857
await patchCodegenAndroidPackage(root, packageJson, report);
4958
}
50-
await removeCodegenAppLevelCode(root, packageJson);
59+
60+
if (!codegenCLISupportsSource) {
61+
await removeCodegenAppLevelCode(root, packageJson);
62+
}
5163

5264
report.success('Generated native code with codegen');
5365
} catch (e: unknown) {

Diff for: packages/react-native-builder-bob/src/targets/codegen/patches/removeCodegenAppLevelCode.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs-extra';
22
import path from 'path';
33
import { CODEGEN_DOCS } from './patchCodegenAndroidPackage';
4+
import { spawn } from '../../../utils/spawn';
45

56
const FILES_TO_REMOVE = [
67
'RCTAppDependencyProvider.h',
@@ -68,3 +69,18 @@ export async function removeCodegenAppLevelCode(
6869

6970
await Promise.allSettled([...androidPromises, ...iosPromises]);
7071
}
72+
73+
/**
74+
* Codegen generates a different set of files if the target is an app instead.
75+
* The following commit adds support for a --source argument to support calling codegen as a library:
76+
* https://github.com/facebook/react-native/commit/98b8f178110472e5fed97de80766c03b0b5e988c
77+
* Here we just check if the --source argument is supported.
78+
*/
79+
export async function getCodegenCLISourceSupport(): Promise<boolean> {
80+
const codegenCLIHelpOutput = await spawn('npx', [
81+
'@react-native-community/cli',
82+
'codegen',
83+
'--help',
84+
]);
85+
return codegenCLIHelpOutput.includes('--source');
86+
}

0 commit comments

Comments
 (0)