Skip to content

Commit

Permalink
Use the new CLI properties for codegen the legacy components
Browse files Browse the repository at this point in the history
Summary:
This change updates the way in which we consume the list of components from the `react-native.config.js` files so that it is aligned to what Android does.

## Changelog:
[iOS][Changed] - Update how the `react-native.config.js` is consumed to add elements in the interop layer.

Reviewed By: cortinico

Differential Revision: D43875395

fbshipit-source-id: e359c98a9144f4efe62967096d48318491718958
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Mar 8, 2023
1 parent 7858a21 commit a055e07
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions scripts/codegen/generate-legacy-interop-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const yargs = require('yargs');
const fs = require('fs');

const CONFIG_FILE_NAME = 'react-native.config.js';
const LEGACY_COMPONENTS_FIELD = 'unstable_reactLegacyComponent';
const PROJECT_FIELD = 'project';
const IOS_FIELD = 'ios';
const LEGACY_COMPONENTS_FIELD = 'unstable_reactLegacyComponentNames';
const OUTPUT_FILE_NAME = 'RCTLegacyInteropComponents.mm';

const argv = yargs
Expand Down Expand Up @@ -57,33 +59,65 @@ ${components}
// eslint-enable duplicate-license-header
}

function extractComponentsNames(reactNativeConfig) {
if (!reactNativeConfig) {
console.log('No reactNativeConfig in the react-native.config.js file');
return null;
}

const project = reactNativeConfig[PROJECT_FIELD];

if (!project) {
console.log(`No ${PROJECT_FIELD} in the react-native config`);
return null;
}

const ios = project[IOS_FIELD];

if (!ios) {
console.log(
`No ${IOS_FIELD} in the ${PROJECT_FIELD} object of the config file`,
);
return null;
}

const componentNames = ios[LEGACY_COMPONENTS_FIELD];

if (!componentNames) {
console.log(
`No '${LEGACY_COMPONENTS_FIELD}' field in the ${PROJECT_FIELD}.${IOS_FIELD} object`,
);
return null;
}
return componentNames;
}

function generateRCTLegacyInteropComponents() {
const configFilePath = `${appRoot}/${CONFIG_FILE_NAME}`;
let reactNativeConfig = null;
try {
reactNativeConfig = require(configFilePath);
} catch (error) {
console.log(`No ${configFilePath}. Skip LegacyInterop generation`);
return;
}

if (reactNativeConfig && reactNativeConfig[LEGACY_COMPONENTS_FIELD]) {
let componentsArray = reactNativeConfig[LEGACY_COMPONENTS_FIELD].map(
name => `\t\t\t@"${name}",`,
);
// Remove the last comma
if (componentsArray.length > 0) {
componentsArray[componentsArray.length - 1] = componentsArray[
componentsArray.length - 1
].slice(0, -1);
}

const filePath = `${outputPath}/${OUTPUT_FILE_NAME}`;
fs.writeFileSync(filePath, fileBody(componentsArray.join('\n')));
} else {
console.log(
`No '${LEGACY_COMPONENTS_FIELD}' field. Skip LegacyInterop generation`,
);
const componentNames = extractComponentsNames(reactNativeConfig);
if (!componentNames) {
console.log('Skip LegacyInterop generation');
return;
}

let componentsArray = componentNames.map(name => `\t\t\t@"${name}",`);
// Remove the last comma
if (componentsArray.length > 0) {
componentsArray[componentsArray.length - 1] = componentsArray[
componentsArray.length - 1
].slice(0, -1);
}

const filePath = `${outputPath}/${OUTPUT_FILE_NAME}`;
fs.writeFileSync(filePath, fileBody(componentsArray.join('\n')));
}

generateRCTLegacyInteropComponents();

0 comments on commit a055e07

Please sign in to comment.