Skip to content

Commit

Permalink
fix: bob codegen deletes view specs when codegen type is all
Browse files Browse the repository at this point in the history
  • Loading branch information
atlj committed Jan 28, 2025
1 parent 0bacd58 commit b43403f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ const mockReport: Report = {
success: console.log,
};

const mockJavaSpec = `
const mockJavaModuleSpec = `
/**
* Some comment
*/
package com.bobtest;
package com.facebook.fbreact.specs;
import com.example.exampleimport;
Expand All @@ -38,20 +38,36 @@ class SomeClass {
}
}`;

const mockJavaViewSpec = `
/**
* Some comment
*/
package com.facebook.react.viewmanagers;
public interface SomeInterface<T extends View> {
void setColor(T view, @Nullable String value);
}
`;

const mockProjectPath = path.resolve(__dirname, 'mockProject');
const mockCodegenSpecsPath = path.resolve(
const mockCodegenModuleSpecsPath = path.resolve(
mockProjectPath,
'android/generated/java/com/facebook/fbreact/specs'
);
const mockCodegenViewSpecsPath = path.resolve(
mockProjectPath,
'android/generated/java/com/facebook/react/viewmanagers'
);

describe('patchCodegenAndroidPackage', () => {
beforeEach(() => {
mockfs({
[mockProjectPath]: {
'package.json': JSON.stringify(mockPackageJson),
},
[mockCodegenSpecsPath]: {
'NativeBobtestSpec.java': mockJavaSpec,
[mockCodegenModuleSpecsPath]: {
'NativeBobtestSpec.java': mockJavaModuleSpec,
},
});
});
Expand Down Expand Up @@ -101,6 +117,40 @@ describe('patchCodegenAndroidPackage', () => {
mockReport
);

expect(await fs.pathExists(mockCodegenSpecsPath)).toBe(false);
expect(await fs.pathExists(mockCodegenModuleSpecsPath)).toBe(false);
});

it("doesn't delete the view manager specs", async () => {
const mockPackageJsonWithTypeAll = {
...mockPackageJson,
codegenConfig: {
...mockPackageJson.codegenConfig,
type: 'all',
},
};

mockfs({
[mockProjectPath]: {
'package.json': JSON.stringify(mockPackageJsonWithTypeAll),
},
[mockCodegenModuleSpecsPath]: {
'NativeBobtestSpec.java': mockJavaModuleSpec,
},
[mockCodegenViewSpecsPath]: {
'BobtestViewManagerInterface.java': mockJavaViewSpec,
},
});

await patchCodegenAndroidPackage(
mockProjectPath,
mockPackageJsonWithTypeAll,
mockReport
);

expect(
await fs.pathExists(
path.join(mockCodegenViewSpecsPath, 'BobtestViewManagerInterface.java')
)
).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function patchCodegenAndroidPackage(

if (!(await fs.pathExists(codegenAndroidPath))) {
throw new Error(
`The codegen android path defined in your package.json: ${codegenAndroidPath} doesnt' exist.`
`The codegen android path defined in your package.json: ${codegenAndroidPath} doesn't exist.`
);
}

Expand Down Expand Up @@ -89,7 +89,19 @@ export async function patchCodegenAndroidPackage(
})
);

await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook'), {
recursive: true,
});
if (
await fs.pathExists(
path.resolve(codegenAndroidPath, 'java/com/facebook/react/viewmanagers')
)
) {
// Keep the view managers
await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook/fbreact'), {
recursive: true,
});
} else {
// Delete the entire facebook namespace
await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook'), {
recursive: true,
});
}
}

0 comments on commit b43403f

Please sign in to comment.