diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index e0b7c0dd56bb33..8cd53b9d9ffcf2 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -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) { @@ -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), @@ -321,17 +317,30 @@ function cleanupEmptyFilesAndFolders(filepath) { * @throws If it can't find a cli for the CodeGen. */ function execute(projectRoot, outputPath) { - buildCodegenIfNeeded(); - try { - const libraries = findCodegenEnabledLibraries(projectRoot); + const pkgJson = readPkgJsonInDirectory(projectRoot); + if (!(pkgJson.codegenConfig && typeof pkgJson.codegenConfig === 'object')) { + throw `[Codegen] Error: package.json in ${pkgJsonPath} must contain "codegenConfig" 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);