Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use proper codegen types #697

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
},
"codegenConfig": {
"name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec",
"type": "all",
"type": "<%- project.view ? 'components': 'modules' -%>",
"jsSrcsDir": "src",
"outputDir": {
"ios": "ios/generated",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, it, describe, beforeEach, afterEach } from '@jest/globals';
import fs from 'fs-extra';
import path from 'node:path';
import { patchCodegen } from '../utils/patchCodegen';
import { patchCodegenAndroidPackage } from '../utils/patchCodegenAndroidPackage';
import mockfs from 'mock-fs';
import type { Report } from '../types';

Expand Down Expand Up @@ -44,7 +44,7 @@ const mockCodegenSpecsPath = path.resolve(
'android/generated/java/com/facebook/fbreact/specs'
);

describe('patchCodegen', () => {
describe('patchCodegenAndroidPackage', () => {
beforeEach(() => {
mockfs({
[mockProjectPath]: {
Expand All @@ -61,7 +61,11 @@ describe('patchCodegen', () => {
});

it('moves the files to correct dir', async () => {
await patchCodegen(mockProjectPath, mockPackageJson, mockReport);
await patchCodegenAndroidPackage(
mockProjectPath,
mockPackageJson,
mockReport
);

const expectedDir = path.resolve(
mockProjectPath,
Expand All @@ -72,7 +76,11 @@ describe('patchCodegen', () => {
});

it('replaces the package name in the files', async () => {
await patchCodegen(mockProjectPath, mockPackageJson, mockReport);
await patchCodegenAndroidPackage(
mockProjectPath,
mockPackageJson,
mockReport
);

const expectedDir = path.resolve(
mockProjectPath,
Expand All @@ -87,7 +95,11 @@ describe('patchCodegen', () => {
});

it('removes the old package dir', async () => {
await patchCodegen(mockProjectPath, mockPackageJson, mockReport);
await patchCodegenAndroidPackage(
mockProjectPath,
mockPackageJson,
mockReport
);

expect(await fs.pathExists(mockCodegenSpecsPath)).toBe(false);
});
Expand Down
15 changes: 13 additions & 2 deletions packages/react-native-builder-bob/src/targets/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import kleur from 'kleur';
import type { Input } from '../types';
import { patchCodegen } from '../utils/patchCodegen';
import { patchCodegenAndroidPackage } from '../utils/patchCodegenAndroidPackage';
import fs from 'fs-extra';
import path from 'path';
import del from 'del';
Expand Down Expand Up @@ -32,10 +32,21 @@ export default async function build({ root, report }: Options) {
await del([codegenAndroidPath]);
}

const codegenType = packageJson.codegenConfig?.type;

if (codegenType === undefined) {
report.error(
"Couldn't find the 'type' value in 'codegenConfig'. Please check your package.json's 'codegenConfig' property and make sure 'type' is defined. https://reactnative.dev/docs/the-new-architecture/using-codegen#configuring-codegen"
);
process.exit(1);
}

try {
await runRNCCli(['codegen']);

await patchCodegen(root, packageJson, report);
if (codegenType === 'modules' || codegenType === 'all') {
await patchCodegenAndroidPackage(root, packageJson, report);
}

report.success('Generated native code with codegen');
} catch (e: unknown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CODEGEN_DOCS =
* @throws if codegenConfig.outputDir.android or codegenConfig.android.javaPackageName is not defined in package.json
* @throws if the codegenAndroidPath does not exist
*/
export async function patchCodegen(
export async function patchCodegenAndroidPackage(
projectPath: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJson: any,
Expand Down
Loading