Skip to content

Commit

Permalink
Refactor generate-artifacts-executor.js: return libraries array inste…
Browse files Browse the repository at this point in the history
…ad of using an inout argument (#41531)

Summary:
Pull Request resolved: #41531

This diff converts in-out `libraries` argument of codegen library lookup functions to a normal return value. This makes these functions simpler to reason about, and simplifies subsequent refactors.

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D51111416

fbshipit-source-id: 12b5dda4d326e3f1c866c16f7bcd17080be54b58
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Nov 21, 2023
1 parent a1b67f9 commit d1e03f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,16 @@ describe('generateCode', () => {

describe('extractLibrariesFromJSON', () => {
it('throws if in react-native and no dependencies found', () => {
let libraries = [];
let configFile = {};
expect(() => {
underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
);
underTest._extractLibrariesFromJSON(configFile, codegenConfigKey);
}).toThrow();
});

it('it skips if not into react-native and no dependencies found', () => {
let libraries = [];
let configFile = {};

underTest._extractLibrariesFromJSON(
let libraries = underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
'some-node-module',
'node_modules/some',
Expand All @@ -109,11 +101,9 @@ describe('extractLibrariesFromJSON', () => {
});

it('extracts a single dependency when config has no libraries', () => {
let libraries = [];
let configFile = fixtures.noLibrariesConfigFile;
underTest._extractLibrariesFromJSON(
let libraries = underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
'my-app',
'.',
Expand All @@ -131,11 +121,9 @@ describe('extractLibrariesFromJSON', () => {

it("extract codegenConfig when it's empty", () => {
const configFile = {codegenConfig: {libraries: []}};
let libraries = [];
underTest._extractLibrariesFromJSON(
let libraries = underTest._extractLibrariesFromJSON(
configFile,
codegenConfigKey,
libraries,
reactNativeDependencyName,
rootPath,
);
Expand All @@ -144,10 +132,8 @@ describe('extractLibrariesFromJSON', () => {

it('extract codegenConfig when dependency is one', () => {
const configFile = fixtures.singleLibraryCodegenConfig;
let libraries = [];
underTest._extractLibrariesFromJSON(
let libraries = underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
reactNativeDependencyName,
rootPath,
Expand All @@ -167,10 +153,8 @@ describe('extractLibrariesFromJSON', () => {
const configFile = fixtures.multipleLibrariesCodegenConfig;
const myDependency = 'my-dependency';
const myDependencyPath = path.join(__dirname, myDependency);
let libraries = [];
underTest._extractLibrariesFromJSON(
let libraries = underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
myDependency,
myDependencyPath,
Expand Down
164 changes: 74 additions & 90 deletions packages/react-native/scripts/codegen/generate-artifacts-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,18 @@ function printDeprecationWarningIfNeeded(dependency) {
function extractLibrariesFromConfigurationArray(
configFile,
codegenConfigKey,
libraries,
dependencyPath,
) {
configFile[codegenConfigKey].libraries.forEach(config => {
const libraryConfig = {
return configFile[codegenConfigKey].libraries.map(config => {
return {
config,
libraryPath: dependencyPath,
};
libraries.push(libraryConfig);
});
}

function extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
dependency,
dependencyPath,
Expand All @@ -136,29 +133,29 @@ function extractLibrariesFromJSON(
if (isBlocking) {
throw `[Codegen] Error: Could not find codegen config for ${dependency} .`;
}
return;
return [];
}

console.log(`[Codegen] Found ${dependency}`);
if (configFile[codegenConfigKey].libraries == null) {
var config = configFile[codegenConfigKey];
libraries.push({
config,
libraryPath: dependencyPath,
});
return [
{
config,
libraryPath: dependencyPath,
},
];
} else {
printDeprecationWarningIfNeeded(dependency);
extractLibrariesFromConfigurationArray(
return extractLibrariesFromConfigurationArray(
configFile,
codegenConfigKey,
libraries,
dependencyPath,
);
}
}

function handleReactNativeCoreLibraries(
libraries,
codegenConfigFilename,
codegenConfigKey,
) {
Expand All @@ -173,11 +170,10 @@ function handleReactNativeCoreLibraries(
throw '[Codegen] Error: Could not find config file for react-native.';
}
const reactNativeConfigFile = JSON.parse(fs.readFileSync(reactNativePkgJson));
extractLibrariesFromJSON(reactNativeConfigFile, libraries, codegenConfigKey);
return extractLibrariesFromJSON(reactNativeConfigFile, codegenConfigKey);
}

function handleThirdPartyLibraries(
libraries,
baseCodegenConfigFileDir,
dependencies,
codegenConfigFilename,
Expand All @@ -192,31 +188,30 @@ function handleThirdPartyLibraries(
);

// Handle third-party libraries
Object.keys(dependencies).forEach(dependency => {
return Object.keys(dependencies).flatMap(dependency => {
if (dependency === REACT_NATIVE_DEPENDENCY_NAME) {
// react-native should already be added.
return;
return [];
}
const codegenConfigFileDir = path.join(configDir, dependency);
const configFilePath = path.join(
codegenConfigFileDir,
codegenConfigFilename,
);
if (fs.existsSync(configFilePath)) {
const configFile = JSON.parse(fs.readFileSync(configFilePath));
extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
dependency,
codegenConfigFileDir,
);
if (!fs.existsSync(configFilePath)) {
return [];
}
const configFile = JSON.parse(fs.readFileSync(configFilePath));
return extractLibrariesFromJSON(
configFile,
codegenConfigKey,
dependency,
codegenConfigFileDir,
);
});
}

function handleLibrariesFromReactNativeConfig(
libraries,
codegenConfigKey,
codegenConfigFilename,
appRootDir,
Expand All @@ -229,54 +224,51 @@ function handleLibrariesFromReactNativeConfig(

const rnConfigFilePath = path.resolve(appRootDir, rnConfigFileName);

if (fs.existsSync(rnConfigFilePath)) {
const rnConfig = require(rnConfigFilePath);

if (rnConfig.dependencies != null) {
Object.keys(rnConfig.dependencies).forEach(name => {
const dependencyConfig = rnConfig.dependencies[name];

if (dependencyConfig.root) {
const codegenConfigFileDir = path.resolve(
appRootDir,
dependencyConfig.root,
);
const configFilePath = path.join(
codegenConfigFileDir,
codegenConfigFilename,
);
const pkgJsonPath = path.join(codegenConfigFileDir, 'package.json');

if (fs.existsSync(configFilePath)) {
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath));
const configFile = JSON.parse(fs.readFileSync(configFilePath));
extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
pkgJson.name,
codegenConfigFileDir,
);
}
}
});
}
if (!fs.existsSync(rnConfigFilePath)) {
return [];
}
const rnConfig = require(rnConfigFilePath);

if (rnConfig.dependencies == null) {
return [];
}
return Object.keys(rnConfig.dependencies).flatMap(name => {
const dependencyConfig = rnConfig.dependencies[name];

if (!dependencyConfig.root) {
return [];
}
const codegenConfigFileDir = path.resolve(
appRootDir,
dependencyConfig.root,
);
const configFilePath = path.join(
codegenConfigFileDir,
codegenConfigFilename,
);
if (!fs.existsSync(configFilePath)) {
return [];
}
const pkgJsonPath = path.join(codegenConfigFileDir, 'package.json');
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath));
const configFile = JSON.parse(fs.readFileSync(configFilePath));

return extractLibrariesFromJSON(
configFile,
codegenConfigKey,
pkgJson.name,
codegenConfigFileDir,
);
});
}

function handleInAppLibraries(
libraries,
pkgJson,
codegenConfigKey,
appRootDir,
) {
function handleInAppLibraries(pkgJson, codegenConfigKey, appRootDir) {
console.log(
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app',
);

extractLibrariesFromJSON(
return extractLibrariesFromJSON(
pkgJson,
libraries,
codegenConfigKey,
pkgJson.name,
appRootDir,
Expand Down Expand Up @@ -433,29 +425,21 @@ function findCodegenEnabledLibraries(
) {
const pkgJson = readPackageJSON(appRootDir);
const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies};
const libraries = [];

handleReactNativeCoreLibraries(
libraries,
codegenConfigFilename,
codegenConfigKey,
);
handleThirdPartyLibraries(
libraries,
baseCodegenConfigFileDir,
dependencies,
codegenConfigFilename,
codegenConfigKey,
);
handleLibrariesFromReactNativeConfig(
libraries,
codegenConfigKey,
codegenConfigFilename,
appRootDir,
);
handleInAppLibraries(libraries, pkgJson, codegenConfigKey, appRootDir);

return libraries;
return [
...handleReactNativeCoreLibraries(codegenConfigFilename, codegenConfigKey),
...handleThirdPartyLibraries(
baseCodegenConfigFileDir,
dependencies,
codegenConfigFilename,
codegenConfigKey,
),
...handleLibrariesFromReactNativeConfig(
codegenConfigKey,
codegenConfigFilename,
appRootDir,
),
...handleInAppLibraries(pkgJson, codegenConfigKey, appRootDir),
];
}

// It removes all the empty files and empty folders
Expand Down

0 comments on commit d1e03f5

Please sign in to comment.