Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit ae7ff33

Browse files
smarter node modules path resolving (#2558)
Co-authored-by: Djordje Dimitrijev <v-djordjed@microsoft.com>
1 parent a801da4 commit ae7ff33

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

android/codepush.gradle

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,41 @@ void runBefore(String dependentTaskName, Task task) {
1717
}
1818
}
1919

20+
/**
21+
* Finds the path of the installed npm package with the given name using Node's
22+
* module resolution algorithm, which searches "node_modules" directories up to
23+
* the file system root. This handles various cases, including:
24+
*
25+
* - Working in the open-source RN repo:
26+
* Gradle: /path/to/react-native/ReactAndroid
27+
* Node module: /path/to/react-native/node_modules/[package]
28+
*
29+
* - Installing RN as a dependency of an app and searching for hoisted
30+
* dependencies:
31+
* Gradle: /path/to/app/node_modules/react-native/ReactAndroid
32+
* Node module: /path/to/app/node_modules/[package]
33+
*
34+
* - Working in a larger repo (e.g., Facebook) that contains RN:
35+
* Gradle: /path/to/repo/path/to/react-native/ReactAndroid
36+
* Node module: /path/to/repo/node_modules/[package]
37+
*
38+
* The search begins at the given base directory (a File object). The returned
39+
* path is a string.
40+
*/
41+
static def findNodeModulePath(baseDir, packageName) {
42+
def basePath = baseDir.toPath().normalize()
43+
// Node's module resolution algorithm searches up to the root directory,
44+
// after which the base path will be null
45+
while (basePath) {
46+
def candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
47+
if (candidatePath.toFile().exists()) {
48+
return candidatePath.toString()
49+
}
50+
basePath = basePath.getParent()
51+
}
52+
return null
53+
}
54+
2055
android.buildTypes.each { buildType ->
2156
// to prevent incorrect long value restoration from strings.xml we need to wrap it with double quotes
2257
// https://github.com/microsoft/cordova-plugin-code-push/issues/264
@@ -34,11 +69,9 @@ gradle.projectsEvaluated {
3469

3570
def nodeModulesPath;
3671
if (project.hasProperty('nodeModulesPath')) {
37-
nodeModulesPath = project.nodeModulesPath
38-
} else if (config.root) {
39-
nodeModulesPath = Paths.get(config.root.asFile.get().absolutePath, "/node_modules");
72+
nodeModulesPath = "${project.nodeModulesPath}/react-native-code-push"
4073
} else {
41-
nodeModulesPath = "../../node_modules";
74+
nodeModulesPath = findNodeModulePath(projectDir, "react-native-code-push")
4275
}
4376

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

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

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

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

0 commit comments

Comments
 (0)