diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js index 42eeebe7604413..e9b892e69f5d8a 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js @@ -29,7 +29,7 @@ function generateReactCodegenPodspec( appPath /*: string */, appPkgJson /*: $FlowFixMe */, outputPath /*: string */, - baseOutputPath /*: string */, + baseOutputPath /*: ?string */, ) { const inputFiles = getInputFiles(appPath, appPkgJson); const codegenScript = codegenScripts(appPath, baseOutputPath); @@ -76,14 +76,24 @@ function getInputFiles(appPath /*: string */, appPkgJson /*: $FlowFixMe */) { return `[${list}]`; } -function codegenScripts(appPath /*: string */, outputPath /*: string */) { - const relativeAppPath = path.relative(outputPath, appPath); +function codegenScripts( + appPath /*: string */, + outputPath /*: ?string */, +) { + const relativeAppPath = + outputPath != null && outputPath.length > 0 + ? path.relative(outputPath, appPath) + : ''; + const relativeReactNativeRootFolder = + outputPath != null && outputPath.length > 0 + ? path.relative(outputPath, REACT_NATIVE_PACKAGE_ROOT_FOLDER) + : ''; return `<<-SCRIPT pushd "$PODS_ROOT/../" > /dev/null RCT_SCRIPT_POD_INSTALLATION_ROOT=$(pwd) popd >/dev/null -export RCT_SCRIPT_RN_DIR="$RCT_SCRIPT_POD_INSTALLATION_ROOT/${path.relative(outputPath, REACT_NATIVE_PACKAGE_ROOT_FOLDER)}" +export RCT_SCRIPT_RN_DIR="$RCT_SCRIPT_POD_INSTALLATION_ROOT/${relativeReactNativeRootFolder}" export RCT_SCRIPT_APP_PATH="$RCT_SCRIPT_POD_INSTALLATION_ROOT/${relativeAppPath.length === 0 ? '.' : relativeAppPath}" export RCT_SCRIPT_OUTPUT_DIR="$RCT_SCRIPT_POD_INSTALLATION_ROOT" export RCT_SCRIPT_TYPE="withCodegenDiscovery" diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js index 588c5d2b5fd327..2128d90dc2d1b7 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js @@ -63,7 +63,7 @@ const path = require('path'); function execute( projectRoot /*: string */, targetPlatform /*: string */, - baseOutputPath /*: string */, + baseOutputPath /*: ?string */, source /*: string */, runReactNativeCodegen /*: boolean */ = true, ) { @@ -210,29 +210,38 @@ function readOutputDirFromPkgJson( function computeOutputPath( projectRoot /*: string */, - baseOutputPath /*: string */, + optionalBaseOutputPath /*: ?string */, pkgJson /*: $FlowFixMe */, platform /*: string */, -) { - if (baseOutputPath == null) { +) /*: string */ { + let baseOutputPath /*: string */; + if (optionalBaseOutputPath == null) { const outputDirFromPkgJson = readOutputDirFromPkgJson(pkgJson, platform); if (outputDirFromPkgJson != null) { - // $FlowFixMe[reassign-const] baseOutputPath = path.join(projectRoot, outputDirFromPkgJson); } else { - // $FlowFixMe[reassign-const] baseOutputPath = projectRoot; } + } else { + baseOutputPath = optionalBaseOutputPath; } if (pkgJsonIncludesGeneratedCode(pkgJson)) { // Don't create nested directories for libraries to make importing generated headers easier. return baseOutputPath; } if (platform === 'android') { - return defaultOutputPathForAndroid(baseOutputPath); + return defaultOutputPathForAndroid( + baseOutputPath != null && baseOutputPath.length > 0 + ? baseOutputPath + : projectRoot, + ); } if (platform === 'ios') { - return defaultOutputPathForIOS(baseOutputPath); + return defaultOutputPathForIOS( + baseOutputPath != null && baseOutputPath.length > 0 + ? baseOutputPath + : projectRoot, + ); } return baseOutputPath; } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js index 680f8a6d295dc2..77f5cdd543f163 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -98,11 +98,15 @@ function cleanupEmptyFilesAndFolders(filepath /*: string */) { } function readGeneratedAutolinkingOutput( - baseOutputPath /*: string */, + projectRoot /*: string */, + baseOutputPath /*: ?string */, ) /*: $FlowFixMe */ { // NOTE: Generated by scripts/cocoapods/autolinking.rb in list_native_modules (called by use_native_modules) + // The `baseOutputPath` is based on a CLI argument and optional const autolinkingGeneratedPath = path.resolve( - baseOutputPath, + baseOutputPath != null && baseOutputPath.length > 0 + ? baseOutputPath + : projectRoot, 'build/generated/autolinking/autolinking.json', ); if (fs.existsSync(autolinkingGeneratedPath)) { @@ -118,9 +122,12 @@ function readGeneratedAutolinkingOutput( function readReactNativeConfig( projectRoot /*: string */, - baseOutputPath /*: string */, + baseOutputPath /*: ?string */, ) /*: $FlowFixMe */ { - const autolinkingOutput = readGeneratedAutolinkingOutput(baseOutputPath); + const autolinkingOutput = readGeneratedAutolinkingOutput( + projectRoot, + baseOutputPath, + ); const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); if (autolinkingOutput) { return autolinkingOutput; @@ -139,7 +146,7 @@ function readReactNativeConfig( function findCodegenEnabledLibraries( pkgJson /*: $FlowFixMe */, projectRoot /*: string */, - baseOutputPath /*: string */, + baseOutputPath /*: ?string */, reactNativeConfig /*: $FlowFixMe */, ) /*: Array<$FlowFixMe> */ { const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot); @@ -149,7 +156,7 @@ function findCodegenEnabledLibraries( const libraries = [...projectLibraries]; // If we ran autolinking, we shouldn't try to run our own "autolinking-like" // library discovery - if (!readGeneratedAutolinkingOutput(baseOutputPath)) { + if (!readGeneratedAutolinkingOutput(projectRoot, baseOutputPath)) { libraries.push(...findExternalLibraries(pkgJson, projectRoot)); } libraries.push(