From eb7e10bc0e78d57726e2971c9dc566e9323deb69 Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Mon, 27 Nov 2023 09:45:55 -0800 Subject: [PATCH] Refactor generate-artifacts-executor.js: give library lookup functions more accurate names (#41560) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41560 This diff gives some functions and variables more accurate names: 1. `appRoot` -> `projectRoot` 2. `handleThirdPartyLibraries` -> `findExternalLibraries` 3. `handleLibrariesFromReactNativeConfig` -> `findLibrariesFromReactNativeConfig` 4. `handleInAppLibraries` -> `findProjectRootLibraries` It also removes `isAppRootValid` check that checks that `appRoot != null`, it is redundant since `appRoot`(now `projectRoot`) is required by the CLI. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D51309027 fbshipit-source-id: c5e34c2aa788a7795c68697a0fa9ddf0163cec0e --- .../codegen/generate-artifacts-executor.js | 42 +++++++------------ .../scripts/generate-codegen-artifacts.js | 4 +- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index 17db4e48c4f..e0b7c0dd56b 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -45,15 +45,6 @@ function isReactNativeCoreLibrary(libraryName) { return libraryName in CORE_LIBRARIES_WITH_OUTPUT_FOLDER; } -function isAppRootValid(appRootDir) { - if (appRootDir == null) { - console.error('Missing path to React Native application'); - process.exitCode = 1; - return false; - } - return true; -} - function readPkgJsonInDirectory(dir) { const pkgJsonPath = path.join(dir, 'package.json'); if (!fs.existsSync(pkgJsonPath)) { @@ -129,7 +120,7 @@ function extractLibrariesFromJSON(configFile, dependencyPath) { } } -function handleThirdPartyLibraries(pkgJson) { +function findExternalLibraries(pkgJson) { const dependencies = { ...pkgJson.dependencies, ...pkgJson.devDependencies, @@ -154,12 +145,12 @@ function handleThirdPartyLibraries(pkgJson) { }); } -function handleInAppLibraries(pkgJson, appRootDir) { +function findProjectRootLibraries(pkgJson, projectRoot) { console.log( '\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app', ); - return extractLibrariesFromJSON(pkgJson, appRootDir); + return extractLibrariesFromJSON(pkgJson, projectRoot); } // CodeGen @@ -184,8 +175,11 @@ function buildCodegenIfNeeded() { }); } -function computeIOSOutputDir(outputPath, appRootDir) { - return path.join(outputPath ? outputPath : appRootDir, 'build/generated/ios'); +function computeIOSOutputDir(outputPath, projectRoot) { + return path.join( + outputPath ? outputPath : projectRoot, + 'build/generated/ios', + ); } function generateSchemaInfo(library) { @@ -270,11 +264,11 @@ function createComponentProvider(schemas) { console.log(`Generated provider in: ${outputDir}`); } -function findCodegenEnabledLibraries(appRootDir) { - const pkgJson = readPkgJsonInDirectory(appRootDir); +function findCodegenEnabledLibraries(projectRoot) { + const pkgJson = readPkgJsonInDirectory(projectRoot); return [ - ...handleThirdPartyLibraries(pkgJson), - ...handleInAppLibraries(pkgJson, appRootDir), + ...findExternalLibraries(pkgJson), + ...findProjectRootLibraries(pkgJson, projectRoot), ]; } @@ -320,28 +314,24 @@ function cleanupEmptyFilesAndFolders(filepath) { * - setups the CLI to generate the code * - generate the code * - * @parameter appRootDir: the directory with the app source code, where the package.json lives. + * @parameter projectRoot: the directory with the app source code, where the package.json lives. * @parameter outputPath: the base output path for the CodeGen. * @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) { - if (!isAppRootValid(appRootDir)) { - return; - } - +function execute(projectRoot, outputPath) { buildCodegenIfNeeded(); try { - const libraries = findCodegenEnabledLibraries(appRootDir); + const libraries = findCodegenEnabledLibraries(projectRoot); if (libraries.length === 0) { console.log('[Codegen] No codegen-enabled libraries found.'); return; } - const iosOutputDir = computeIOSOutputDir(outputPath, appRootDir); + const iosOutputDir = computeIOSOutputDir(outputPath, projectRoot); const schemaInfos = generateSchemaInfos(libraries); generateNativeCode(iosOutputDir, schemaInfos); diff --git a/packages/react-native/scripts/generate-codegen-artifacts.js b/packages/react-native/scripts/generate-codegen-artifacts.js index 186c1f39c32..00849c8091b 100644 --- a/packages/react-native/scripts/generate-codegen-artifacts.js +++ b/packages/react-native/scripts/generate-codegen-artifacts.js @@ -15,11 +15,11 @@ const yargs = require('yargs'); const argv = yargs .option('p', { alias: 'path', - description: 'Path to React Native application', + description: 'Path to the React Native project root.', }) .option('o', { alias: 'outputPath', - description: 'Path where generated artifacts will be output to', + description: 'Path where generated artifacts will be output to.', }) .usage('Usage: $0 -p [path to app]') .demandOption(['p']).argv;