Skip to content

Commit

Permalink
Merge pull request #580 from DataDog/louiszawadzki/rum-2589/improve-r…
Browse files Browse the repository at this point in the history
…n-detection-without-node

Remove usage of node for finding RN in gradle
  • Loading branch information
louiszawadzki authored Jan 4, 2024
2 parents 8884d04 + cc31fd5 commit e57805b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
44 changes: 27 additions & 17 deletions packages/core/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
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 e57805b

Please sign in to comment.