Skip to content

Commit a9546fb

Browse files
authored
fix: vanilla example codegen fails (#665)
### Summary Fixes #662 ### Test plan 1. Create a vanilla example library 2. Make sure `@react-native-community/cli` is added as a dependency 3. make sure `yarn prepare` doesn't throw
1 parent aa4c63b commit a9546fb

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dedent from 'dedent';
55
import kleur from 'kleur';
66
import yargs from 'yargs';
77
import ora from 'ora';
8+
import assert from 'node:assert';
89
import validateNpmPackage from 'validate-npm-package-name';
910
import githubUsername from 'github-username';
1011
import prompts, { type PromptObject } from './utils/prompts';
@@ -790,8 +791,23 @@ async function create(_argv: yargs.Arguments<any>) {
790791
examplePackageJson.dependencies['react-native'];
791792
}
792793

793-
if (arch !== 'legacy' && example === 'vanilla') {
794-
addCodegenBuildScript(folder, options.project.name);
794+
if (example === 'vanilla') {
795+
// React Native doesn't provide the community CLI as a dependency.
796+
// We have to get read the version from the example app and put to the root package json
797+
const exampleCommunityCLIVersion =
798+
examplePackageJson.devDependencies['@react-native-community/cli'];
799+
assert(
800+
exampleCommunityCLIVersion !== undefined,
801+
"The generated example app doesn't have community CLI installed"
802+
);
803+
804+
rootPackageJson.devDependencies = rootPackageJson.devDependencies || {};
805+
rootPackageJson.devDependencies['@react-native-community/cli'] =
806+
exampleCommunityCLIVersion;
807+
808+
if (arch !== 'legacy') {
809+
addCodegenBuildScript(folder, options.project.name);
810+
}
795811
}
796812
}
797813

packages/create-react-native-library/templates/common/$package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
"devDependencies": {
7373
"@commitlint/config-conventional": "^17.0.2",
7474
"@evilmartians/lefthook": "^1.5.0",
75+
<% if (example === 'vanilla') { -%>
76+
"@react-native-community/cli": "15.0.0-alpha.2",
77+
<% } -%>
7578
"@react-native/eslint-config": "^0.73.1",
7679
"@release-it/conventional-changelog": "^5.0.0",
7780
"@types/jest": "^29.5.5",

packages/react-native-builder-bob/src/targets/codegen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default async function build({ root, report }: Options) {
3333
}
3434

3535
try {
36-
await spawn('npx', ['react-native', 'codegen'], {
36+
await spawn('npx', ['@react-native-community/cli', 'codegen'], {
3737
stdio: 'ignore',
3838
});
3939

0 commit comments

Comments
 (0)