Skip to content

Commit

Permalink
fix[codegen]: patch generation directory as described in facebook/rea…
Browse files Browse the repository at this point in the history
  • Loading branch information
wneel committed Dec 21, 2024
1 parent cfd0238 commit 6605f53
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir lib/types",
"build:cjstypes": "tsc -p tsconfig.commonjs.json",
"build:esmtypes": "tsc -p tsconfig.module.json",
"codegen": "npx @react-native-community/cli codegen",
"codegen": "npx @react-native-community/cli codegen && node utils/patchCodegen.mjs",
"build": "npm run build:babel && npm run build:types && npm run codegen",
"prepare": "npm run clean && npm run build"
},
Expand Down Expand Up @@ -73,6 +73,7 @@
"@react-native-community/cli": "15.0.0",
"@types/react": "^18.2.44",
"del-cli": "^5.1.0",
"fs-extra": "^11.2.0",
"react": "18.3.1",
"react-native": "0.76.1",
"react-native-builder-bob": "^0.30.3",
Expand All @@ -87,7 +88,7 @@
},
"codegenConfig": {
"name": "RNGetDeviceLocaleSpec",
"type": "all",
"type": "modules",
"jsSrcsDir": "src",
"outputDir": {
"ios": "ios/generated",
Expand Down
96 changes: 96 additions & 0 deletions utils/patchCodegen.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// https://github.com/callstack/react-native-builder-bob/blob/a2024246378272b49f1727dc4395f2725f3bc616/packages/react-native-builder-bob/src/utils/patchCodegen.ts

import fs from 'fs-extra';
import path from 'path';

/**
* Currently, running react-native codegen generates java files with package name `com.facebook.fbreact.specs`.
* This is a known issue in react-native itself.
* You can find the relevant line here: https://github.com/facebook/react-native/blob/dc460147bb00d6f912cc0a829f8040d85faeeb13/packages/react-native/scripts/codegen/generate-artifacts-executor.js#L459.
* To workaround, this function renames the package name to the one provided in the codegenConfig.
* @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() {

const projectPath = process.cwd();

const packageJsonPath = path.resolve(projectPath, 'package.json');
const packageJson = await fs.readJson(packageJsonPath);

let codegenAndroidPath =
packageJson.codegenConfig.outputDir.android;
if (!codegenAndroidPath) {
throw new Error(
'You need to define codegenConfig.outputDir.android in your package.json'
);
}
codegenAndroidPath = path.resolve(projectPath, codegenAndroidPath);

if (!(await fs.pathExists(codegenAndroidPath))) {
throw new Error(
`Could not find ${codegenAndroidPath}. Make sure you are in the correct directory and react-native codegen works properly.`
);
}

const codegenJavaPackageName =
packageJson.codegenConfig.android.javaPackageName;
if (!codegenJavaPackageName) {
throw new Error(
'You need to define codegenConfig.android.javaPackageName in your package.json'
);
}

const codegenJavaPath = path.resolve(
codegenAndroidPath,
`java/com/facebook/fbreact/specs`
);

// If this issue is ever fixed in react-native, this check will prevent the patching from running.
if (!(await fs.pathExists(codegenJavaPath))) {
console.log(
`Could not find ${codegenJavaPath}. Skipping patching codegen java files.`
);
return;
}

const javaFiles = await fs.readdir(codegenJavaPath);

await Promise.all(
javaFiles.map(async (file) => {
const filePath = path.resolve(codegenJavaPath, file);
const fileContent = await fs.readFile(filePath, 'utf8');

const newFileContent = fileContent.replace(
'package com.facebook.fbreact.specs',
`package ${codegenJavaPackageName}`
);

await fs.writeFile(filePath, newFileContent);
})
);

const newPackagePath = path.resolve(
codegenAndroidPath,
`java/${codegenJavaPackageName.replace(/\./g, '/')}`
);

if (!(await fs.pathExists(newPackagePath))) {
await fs.mkdir(newPackagePath, { recursive: true });
}

await Promise.all(
javaFiles.map(async (file) => {
const filePath = path.resolve(codegenJavaPath, file);
const newFilePath = path.resolve(newPackagePath, file);

await fs.rename(filePath, newFilePath);
})
);

await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook'), {
recursive: true,
});
}

patchCodegen();

0 comments on commit 6605f53

Please sign in to comment.