@@ -17,6 +17,41 @@ void runBefore(String dependentTaskName, Task task) {
17
17
}
18
18
}
19
19
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
+
20
55
android. buildTypes. each { buildType ->
21
56
// to prevent incorrect long value restoration from strings.xml we need to wrap it with double quotes
22
57
// https://github.com/microsoft/cordova-plugin-code-push/issues/264
@@ -34,11 +69,9 @@ gradle.projectsEvaluated {
34
69
35
70
def nodeModulesPath;
36
71
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"
40
73
} else {
41
- nodeModulesPath = " ../../node_modules " ;
74
+ nodeModulesPath = findNodeModulePath(projectDir, " react-native-code-push " )
42
75
}
43
76
44
77
def targetName = variant. name. capitalize()
@@ -71,7 +104,7 @@ gradle.projectsEvaluated {
71
104
generateBundledResourcesHash = tasks. create(
72
105
name : " generateBundledResourcesHash${ targetName} " ,
73
106
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)
75
108
76
109
enabled ! debuggableVariants. contains(variant. name) ?: targetName. toLowerCase(). contains(" release" )
77
110
}
@@ -101,14 +134,14 @@ gradle.projectsEvaluated {
101
134
generateBundledResourcesHash = tasks. create(
102
135
name : " generateBundledResourcesHash${ targetName} " ,
103
136
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)
105
138
}
106
139
107
140
// Make this task run right before the bundle task
108
141
def recordFilesBeforeBundleCommand = tasks. create(
109
142
name : " recordFilesBeforeBundleCommand${ targetName} " ,
110
143
type : Exec ) {
111
- commandLine (* nodeExecutableAndArgs, " ${ nodeModulesPath} /react-native-code-push/ scripts/recordFilesBeforeBundleCommand.js" , resourcesDir, resourcesMapTempFileName)
144
+ commandLine (* nodeExecutableAndArgs, " ${ nodeModulesPath} /scripts/recordFilesBeforeBundleCommand.js" , resourcesDir, resourcesMapTempFileName)
112
145
}
113
146
114
147
recordFilesBeforeBundleCommand. dependsOn(" merge${ targetName} Resources" )
0 commit comments