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
This commit is contained in:
Dmitry Rykun
2023-11-27 09:45:55 -08:00
committed by Facebook GitHub Bot
parent 834447424f
commit eb7e10bc0e
2 changed files with 18 additions and 28 deletions
@@ -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);
@@ -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;