Skip to content

Commit

Permalink
Refactor generate-artifacts-executor.js: remove configFileDir CLI arg…
Browse files Browse the repository at this point in the history
…ument, use node resolution instead (#41557)

Summary:
Pull Request resolved: #41557

The `configFileDir` CLI argument is used to help to find paths to the 3rd party dependencies of the app. Since we only care about dependencies listed in the root `package.json`, we can use `node` resolution instead of having to construct paths manually. In that case `configFileDir` becomes redundant.

Changelog: [iOS][Breaking] - Delete configFileDir CLI argument.

Reviewed By: cipolleschi

Differential Revision: D51303793

fbshipit-source-id: 46cb61197ddf51515af634c8fc6b85a8d218c51e
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Nov 23, 2023
1 parent 1e68e48 commit 8a62d61
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,7 @@ describe('findCodegenEnabledLibraries', () => {
},
}));

const libraries = findCodegenEnabledLibraries(
`${projectDir}/app`,
baseCodegenConfigFileDir,
`package.json`,
'codegenConfig',
);
const libraries = findCodegenEnabledLibraries(`${projectDir}/app`);

expect(libraries).toEqual([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,32 @@ function handleReactNativeCoreLibraries() {
);
}

function handleThirdPartyLibraries(baseCodegenConfigFileDir, dependencies) {
function handleThirdPartyLibraries(pkgJson) {
const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies};
// Determine which of these are codegen-enabled libraries
const configDir =
baseCodegenConfigFileDir ||
path.join(REACT_NATIVE_PACKAGE_ROOT_FOLDER, '..');
console.log(
`\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${configDir}`,
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the project dependencies.',
);

// Handle third-party libraries
return Object.keys(dependencies).flatMap(dependency => {
if (dependency === REACT_NATIVE) {
// react-native should already be added.
return [];
}
let configFile;
try {
configFile = readPkgJsonInDirectory(codegenConfigFileDir);
} catch {
const configFilePath = require.resolve(
path.join(dependency, 'package.json'),
);
const configFile = JSON.parse(fs.readFileSync(configFilePath));
const codegenConfigFileDir = path.dirname(configFilePath);
return extractLibrariesFromJSON(
configFile,
dependency,
codegenConfigFileDir,
);
} catch (e) {
return [];
}
return extractLibrariesFromJSON(
configFile,
dependency,
codegenConfigFileDir,
);
});
}

Expand Down Expand Up @@ -338,12 +338,11 @@ function createComponentProvider(schemas) {
console.log(`Generated provider in: ${outputDir}`);
}

function findCodegenEnabledLibraries(appRootDir, baseCodegenConfigFileDir) {
function findCodegenEnabledLibraries(appRootDir) {
const pkgJson = readPkgJsonInDirectory(appRootDir);
const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies};
return [
...handleReactNativeCoreLibraries(),
...handleThirdPartyLibraries(baseCodegenConfigFileDir, dependencies),
...handleThirdPartyLibraries(pkgJson),
...handleLibrariesFromReactNativeConfig(appRootDir),
...handleInAppLibraries(pkgJson, appRootDir),
];
Expand Down Expand Up @@ -393,23 +392,19 @@ function cleanupEmptyFilesAndFolders(filepath) {
*
* @parameter appRootDir: the directory with the app source code, where the package.json lives.
* @parameter outputPath: the base output path for the CodeGen.
* @parameter baseCodegenConfigFileDir: the directory of the codeGenConfigFile.
* @throws If it can't find a config file for react-native.
* @throws If it can't find a CodeGen configuration in the file.
* @throws If it can't find a cli for the CodeGen.
*/
function execute(appRootDir, outputPath, baseCodegenConfigFileDir) {
function execute(appRootDir, outputPath) {
if (!isAppRootValid(appRootDir)) {
return;
}

buildCodegenIfNeeded();

try {
const libraries = findCodegenEnabledLibraries(
appRootDir,
baseCodegenConfigFileDir,
);
const libraries = findCodegenEnabledLibraries(appRootDir);

if (libraries.length === 0) {
console.log('[Codegen] No codegen-enabled libraries found.');
Expand Down
8 changes: 1 addition & 7 deletions packages/react-native/scripts/generate-codegen-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ const argv = yargs
alias: 'outputPath',
description: 'Path where generated artifacts will be output to',
})
.option('c', {
alias: 'configFileDir',
default: '',
description:
'Path where codegen config files are located (e.g. node_modules dir).',
})
.usage('Usage: $0 -p [path to app]')
.demandOption(['p']).argv;

executor.execute(argv.path, argv.outputPath, argv.configFileDir);
executor.execute(argv.path, argv.outputPath);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def snap_get_script_phases_with_codegen_discovery_with_config_file_dir()
export RCT_SCRIPT_RN_DIR=$RCT_SCRIPT_POD_INSTALLATION_ROOT/../..
export RCT_SCRIPT_APP_PATH=$RCT_SCRIPT_POD_INSTALLATION_ROOT/
export RCT_SCRIPT_CONFIG_FILE_DIR=$RCT_SCRIPT_POD_INSTALLATION_ROOT/node_modules
export RCT_SCRIPT_OUTPUT_DIR=$RCT_SCRIPT_POD_INSTALLATION_ROOT
export RCT_SCRIPT_TYPE=withCodegenDiscovery
Expand All @@ -29,7 +28,6 @@ def snap_get_script_phases_with_codegen_discovery_without_config_file_dir()
export RCT_SCRIPT_RN_DIR=$RCT_SCRIPT_POD_INSTALLATION_ROOT/../..
export RCT_SCRIPT_APP_PATH=$RCT_SCRIPT_POD_INSTALLATION_ROOT/
export RCT_SCRIPT_CONFIG_FILE_DIR=
export RCT_SCRIPT_OUTPUT_DIR=$RCT_SCRIPT_POD_INSTALLATION_ROOT
export RCT_SCRIPT_TYPE=withCodegenDiscovery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def get_script_phases_with_codegen_discovery(options)
export_vars = {
'RCT_SCRIPT_RN_DIR' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:react_native_path]}",
'RCT_SCRIPT_APP_PATH' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:relative_app_root]}",
'RCT_SCRIPT_CONFIG_FILE_DIR' => "#{options[:relative_config_file_dir] != '' ? "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:relative_config_file_dir]}" : ''}",
'RCT_SCRIPT_OUTPUT_DIR' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT",
'RCT_SCRIPT_TYPE' => "withCodegenDiscovery",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ generateCodegenArtifactsFromSchema () {
generateArtifacts () {
describe "Generating codegen artifacts"
pushd "$RCT_SCRIPT_RN_DIR" >/dev/null || exit 1
"$NODE_BINARY" "scripts/generate-codegen-artifacts.js" --path "$RCT_SCRIPT_APP_PATH" --outputPath "$TEMP_OUTPUT_DIR" --configFileDir "$RCT_SCRIPT_CONFIG_FILE_DIR"
"$NODE_BINARY" "scripts/generate-codegen-artifacts.js" --path "$RCT_SCRIPT_APP_PATH" --outputPath "$TEMP_OUTPUT_DIR"
popd >/dev/null || exit 1
}

Expand Down

0 comments on commit 8a62d61

Please sign in to comment.