Skip to content

Commit

Permalink
Introduce "outputDir" property of "codegenConfig" (facebook#41782)
Browse files Browse the repository at this point in the history
Summary:

This diff adds `outputDir` property to `codegenConfig`.
Now codegen output dir is resolved like this:
1. It is set to `outputDir` argument of `generate-codegen-artifacts.js` if it is present.
2. *[New]* It is set to `outputDir` property of `codegenConfig` if it is present.
3. It is set to the project root.

Changelog: [General][Added] - Introduce "outputDir" property of "codegenConfig"

Reviewed By: cipolleschi

Differential Revision: D51494009
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Dec 4, 2023
1 parent ead73de commit cba3c90
Showing 1 changed file with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ function buildCodegenIfNeeded() {
});
}

function computeIOSOutputDir(outputPath, projectRoot) {
return path.join(
outputPath ? outputPath : projectRoot,
'build/generated/ios',
);
function computeIOSOutputDir(outputPath) {
return path.join(outputPath, 'build', 'generated', 'ios');
}

function generateSchemaInfo(library) {
Expand Down Expand Up @@ -264,8 +261,7 @@ function createComponentProvider(schemas) {
console.log(`Generated provider in: ${outputDir}`);
}

function findCodegenEnabledLibraries(projectRoot) {
const pkgJson = readPkgJsonInDirectory(projectRoot);
function findCodegenEnabledLibraries(pkgJson, projectRoot) {
return [
...findExternalLibraries(pkgJson),
...findProjectRootLibraries(pkgJson, projectRoot),
Expand Down Expand Up @@ -321,17 +317,41 @@ function cleanupEmptyFilesAndFolders(filepath) {
* @throws If it can't find a cli for the CodeGen.
*/
function execute(projectRoot, outputPath) {
buildCodegenIfNeeded();

try {
const libraries = findCodegenEnabledLibraries(projectRoot);
console.log(
`[Codegen] Analyzing ${path.join(projectRoot, 'package.json')}`,
);

const pkgJson = readPkgJsonInDirectory(projectRoot);
if (pkgJson.codegenConfig == null) {
console.log(
'[Codegen] The "codegenConfig" field is not defined in package.json. Not running codegen.',
);
return;
}

if (typeof pkgJson.codegenConfig !== 'object') {
throw '[Codegen] "codegenConfig" field must be an Object.';
}

buildCodegenIfNeeded();

const libraries = findCodegenEnabledLibraries(pkgJson, projectRoot);

if (libraries.length === 0) {
console.log('[Codegen] No codegen-enabled libraries found.');
return;
}

const iosOutputDir = computeIOSOutputDir(outputPath, projectRoot);
if (outputPath == null) {
const outputPathOverride = pkgJson.codegenConfig.outputDir;
if (outputPathOverride && typeof outputPathOverride === 'string') {
outputPath = outputPathOverride;
} else {
outputPath = projectRoot;
}
}
const iosOutputDir = computeIOSOutputDir(outputPath);

const schemaInfos = generateSchemaInfos(libraries);
generateNativeCode(iosOutputDir, schemaInfos);
Expand Down

0 comments on commit cba3c90

Please sign in to comment.