Skip to content

Commit

Permalink
smarter node modules path resolving (#2558)
Browse files Browse the repository at this point in the history
Co-authored-by: Djordje Dimitrijev <[email protected]>
  • Loading branch information
wjaykim and DordeDimitrijev authored Jul 8, 2024
1 parent a801da4 commit ae7ff33
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions android/codepush.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,41 @@ void runBefore(String dependentTaskName, Task task) {
}
}

/**
* 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 handles various cases, including:
*
* - Working in the open-source RN repo:
* Gradle: /path/to/react-native/ReactAndroid
* Node module: /path/to/react-native/node_modules/[package]
*
* - Installing RN as a dependency of an app and searching for hoisted
* dependencies:
* Gradle: /path/to/app/node_modules/react-native/ReactAndroid
* Node module: /path/to/app/node_modules/[package]
*
* - Working in a larger repo (e.g., Facebook) that contains RN:
* Gradle: /path/to/repo/path/to/react-native/ReactAndroid
* Node module: /path/to/repo/node_modules/[package]
*
* 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
}

android.buildTypes.each { buildType ->
// to prevent incorrect long value restoration from strings.xml we need to wrap it with double quotes
// https://github.com/microsoft/cordova-plugin-code-push/issues/264
Expand All @@ -34,11 +69,9 @@ gradle.projectsEvaluated {

def nodeModulesPath;
if (project.hasProperty('nodeModulesPath')) {
nodeModulesPath = project.nodeModulesPath
} else if (config.root) {
nodeModulesPath = Paths.get(config.root.asFile.get().absolutePath, "/node_modules");
nodeModulesPath = "${project.nodeModulesPath}/react-native-code-push"
} else {
nodeModulesPath = "../../node_modules";
nodeModulesPath = findNodeModulePath(projectDir, "react-native-code-push")
}

def targetName = variant.name.capitalize()
Expand Down Expand Up @@ -71,7 +104,7 @@ gradle.projectsEvaluated {
generateBundledResourcesHash = tasks.create(
name: "generateBundledResourcesHash${targetName}",
type: Exec) {
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/react-native-code-push/scripts/generateBundledResourcesHash.js", resourcesDir, jsBundleFile, jsBundleDir)
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/scripts/generateBundledResourcesHash.js", resourcesDir, jsBundleFile, jsBundleDir)

enabled !debuggableVariants.contains(variant.name) ?: targetName.toLowerCase().contains("release")
}
Expand Down Expand Up @@ -101,14 +134,14 @@ gradle.projectsEvaluated {
generateBundledResourcesHash = tasks.create(
name: "generateBundledResourcesHash${targetName}",
type: Exec) {
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/react-native-code-push/scripts/generateBundledResourcesHash.js", resourcesDir, jsBundleFile, jsBundleDir, resourcesMapTempFileName)
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/scripts/generateBundledResourcesHash.js", resourcesDir, jsBundleFile, jsBundleDir, resourcesMapTempFileName)
}

// Make this task run right before the bundle task
def recordFilesBeforeBundleCommand = tasks.create(
name: "recordFilesBeforeBundleCommand${targetName}",
type: Exec) {
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/react-native-code-push/scripts/recordFilesBeforeBundleCommand.js", resourcesDir, resourcesMapTempFileName)
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/scripts/recordFilesBeforeBundleCommand.js", resourcesDir, resourcesMapTempFileName)
}

recordFilesBeforeBundleCommand.dependsOn("merge${targetName}Resources")
Expand Down

0 comments on commit ae7ff33

Please sign in to comment.