Skip to content

Commit

Permalink
Remove usage of node for finding RN in gradle for SR package
Browse files Browse the repository at this point in the history
  • Loading branch information
louiszawadzki committed Jan 4, 2024
1 parent edb532b commit cc31fd5
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions packages/react-native-session-replay/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.nio.file.Paths
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
Expand Down Expand Up @@ -35,31 +36,40 @@ apply plugin: 'kotlin-android'
apply plugin: 'org.jlleitschuh.gradle.ktlint'
apply plugin: "io.gitlab.arturbosch.detekt"

/**
* Finds the path of the installed npm package with the given name using Node's
* module resolution algorithm, which searches "node_modules" directories up to
* the file system root.
* This enables us to handle monorepos without requiring the node executable.
*
* The search begins at the given base directory (a File object). The returned
* path is a string.
*/
static def findNodeModulePath(baseDir, packageName) {
def basePath = baseDir.toPath().normalize()
// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
if (candidatePath.toFile().exists()) {
return candidatePath.toString()
}
basePath = basePath.getParent()
}
return null
}

def resolveReactNativeDirectory() {
def reactNativeLocation = rootProject.hasProperty("reactNativeDir") ? rootProject.getProperty("reactNativeDir") : null

if (reactNativeLocation != null) {
return file(reactNativeLocation)
}

try {
// Resolve React Native location with Node
// This will make sure that we get installation location correctly in monorepos
def reactNativePackageJsonPathStdout = new ByteArrayOutputStream()
def reactNativePath = findNodeModulePath(projectDir, "react-native")

exec {
commandLine("node", "-p", "require.resolve('react-native/package.json')")
ignoreExitValue true
standardOutput = reactNativePackageJsonPathStdout
}

def reactNativeFromProjectNodeModules = file(reactNativePackageJsonPathStdout.toString().trim()).getParentFile();

if (reactNativeFromProjectNodeModules.exists()) {
return reactNativeFromProjectNodeModules
}
} catch (e) {
// Ignore
if (reactNativePath != null) {
return file(reactNativePath)
}

throw new Exception(
Expand Down

0 comments on commit cc31fd5

Please sign in to comment.