From 8a62d6135f5ac8c01aa8103ddd91f94973dd6cd3 Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Thu, 23 Nov 2023 06:07:28 -0800 Subject: [PATCH] Refactor generate-artifacts-executor.js: remove configFileDir CLI argument, use node resolution instead (#41557) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../generate-artifacts-executor-test.js | 7 +--- .../codegen/generate-artifacts-executor.js | 41 ++++++++----------- .../scripts/generate-codegen-artifacts.js | 8 +--- .../__tests__/script_phases.snap.rb | 2 - .../react_native_pods_utils/script_phases.rb | 1 - .../react_native_pods_utils/script_phases.sh | 2 +- 6 files changed, 21 insertions(+), 40 deletions(-) diff --git a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js index d5e45257fc270e..f195b66f8210f3 100644 --- a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js +++ b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js @@ -173,12 +173,7 @@ describe('findCodegenEnabledLibraries', () => { }, })); - const libraries = findCodegenEnabledLibraries( - `${projectDir}/app`, - baseCodegenConfigFileDir, - `package.json`, - 'codegenConfig', - ); + const libraries = findCodegenEnabledLibraries(`${projectDir}/app`); expect(libraries).toEqual([ { diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index a4c07c487200f9..d72d427c89b66a 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -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, - ); }); } @@ -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), ]; @@ -393,12 +392,11 @@ 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; } @@ -406,10 +404,7 @@ function execute(appRootDir, outputPath, baseCodegenConfigFileDir) { buildCodegenIfNeeded(); try { - const libraries = findCodegenEnabledLibraries( - appRootDir, - baseCodegenConfigFileDir, - ); + const libraries = findCodegenEnabledLibraries(appRootDir); if (libraries.length === 0) { console.log('[Codegen] No codegen-enabled libraries found.'); diff --git a/packages/react-native/scripts/generate-codegen-artifacts.js b/packages/react-native/scripts/generate-codegen-artifacts.js index bcbc8aaf039f28..186c1f39c32dbc 100644 --- a/packages/react-native/scripts/generate-codegen-artifacts.js +++ b/packages/react-native/scripts/generate-codegen-artifacts.js @@ -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); diff --git a/packages/react-native/scripts/react_native_pods_utils/__tests__/script_phases.snap.rb b/packages/react-native/scripts/react_native_pods_utils/__tests__/script_phases.snap.rb index ff4604b259bef4..bc216b1315d90a 100644 --- a/packages/react-native/scripts/react_native_pods_utils/__tests__/script_phases.snap.rb +++ b/packages/react-native/scripts/react_native_pods_utils/__tests__/script_phases.snap.rb @@ -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 @@ -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 diff --git a/packages/react-native/scripts/react_native_pods_utils/script_phases.rb b/packages/react-native/scripts/react_native_pods_utils/script_phases.rb index 78a381c403a31b..f5dd2ed708312f 100644 --- a/packages/react-native/scripts/react_native_pods_utils/script_phases.rb +++ b/packages/react-native/scripts/react_native_pods_utils/script_phases.rb @@ -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", } diff --git a/packages/react-native/scripts/react_native_pods_utils/script_phases.sh b/packages/react-native/scripts/react_native_pods_utils/script_phases.sh index aff355264ff7bc..847702fdb0dabd 100755 --- a/packages/react-native/scripts/react_native_pods_utils/script_phases.sh +++ b/packages/react-native/scripts/react_native_pods_utils/script_phases.sh @@ -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 }