@@ -82,7 +82,7 @@ plugins.withId('com.android.application') {
8282 injected. execOps. exec {
8383 workingDir reactRoot
8484
85- def cliPackage = " posthog-cli "
85+ def cliPackage = resolvePostHogCliPackagePath(reactRoot)
8686 def args = [cliPackage]
8787
8888 args. addAll([" exp" , " hermes" , " clone" ,
@@ -103,7 +103,7 @@ plugins.withId('com.android.application') {
103103 injected. execOps. exec {
104104 workingDir reactRoot
105105
106- def cliPackage = " posthog-cli "
106+ def cliPackage = resolvePostHogCliPackagePath(reactRoot)
107107 def args = [cliPackage]
108108
109109 def sourcemapDir = sourcemapOutput. getParent()
@@ -157,6 +157,67 @@ plugins.withId('com.android.application') {
157157 }
158158}
159159
160+ def resolvePostHogCliPackagePath (reactRoot ) {
161+ // First, try to find @posthog/cli folder from require.resolve
162+ try {
163+ def resolvedPath = new File ([" node" , " --print" , " require.resolve('@posthog/cli/package.json')" ]. execute(null , rootDir). text. trim()). getParentFile()
164+ if (resolvedPath != null && resolvedPath. exists()) {
165+ def runPostHogCliFile = new File (resolvedPath, " run-posthog-cli.js" )
166+ if (runPostHogCliFile. exists()) {
167+ project. logger. info(" resolved posthog-cli dynamically: `${ runPostHogCliFile.getAbsolutePath()} `" )
168+ return runPostHogCliFile. getAbsolutePath()
169+ }
170+ }
171+ } catch (Throwable ignored) {}
172+
173+ // Second, check if @posthog/cli exists in $reactRoot/node_modules
174+ def nodeModulesPath = new File (" $reactRoot /node_modules/@posthog/cli" )
175+ if (nodeModulesPath. exists()) {
176+ def runPostHogCliFile = new File (nodeModulesPath, " run-posthog-cli.js" )
177+ if (runPostHogCliFile. exists()) {
178+ project. logger. info(" resolved posthog-cli hard-coded path (yarn or npm): `${ runPostHogCliFile.getAbsolutePath()} `" )
179+ return runPostHogCliFile. getAbsolutePath()
180+ }
181+ }
182+
183+ // Third, check for pnpm installation with node_modules/.bin/posthog-cli
184+ def pnpmBinPath = new File (" $reactRoot /node_modules/.bin/posthog-cli" )
185+ if (pnpmBinPath. exists()) {
186+ project. logger. info(" resolved posthog-cli hard-coded path (pnpm): `${ pnpmBinPath.getAbsolutePath()} `" )
187+ return pnpmBinPath. getAbsolutePath()
188+ }
189+
190+ // Fourth, check using npm root for local installation
191+ try {
192+ def npmRoot = [" npm" , " root" ]. execute(null , rootDir). text. trim()
193+ def npmPostHogCliPath = new File (npmRoot, " @posthog/cli" )
194+ if (npmPostHogCliPath. exists()) {
195+ def runPostHogCliFile = new File (npmPostHogCliPath, " run-posthog-cli.js" )
196+ if (runPostHogCliFile. exists()) {
197+ project. logger. info(" resolved posthog-cli via npm root: `${ runPostHogCliFile.getAbsolutePath()} `" )
198+ return runPostHogCliFile. getAbsolutePath()
199+ }
200+ }
201+ } catch (Throwable ignored) {}
202+
203+ // Fifth, check for global npm installation of @posthog/cli
204+ try {
205+ def globalPrefix = [" npm" , " prefix" , " -g" ]. execute(). text. trim()
206+ def globalPostHogCliPath = new File (globalPrefix, " lib/node_modules/@posthog/cli" )
207+ if (globalPostHogCliPath. exists()) {
208+ def runPostHogCliFile = new File (globalPostHogCliPath, " run-posthog-cli.js" )
209+ if (runPostHogCliFile. exists()) {
210+ project. logger. info(" resolved posthog-cli from global npm installation: `${ runPostHogCliFile.getAbsolutePath()} `" )
211+ return runPostHogCliFile. getAbsolutePath()
212+ }
213+ }
214+ } catch (Throwable ignored) {}
215+
216+ // Finally, fallback to global install
217+ project. logger. info(" falling back to global posthog-cli" )
218+ return " posthog-cli"
219+ }
220+
160221def resolvePostHogReactNativeSDKPath (reactRoot ) {
161222 def resolvedPath = null
162223 try {
0 commit comments