Skip to content

Commit 8368cbc

Browse files
authored
fix: posthog-cli finds the correct path on android gradle plugin (#2764)
1 parent bb64696 commit 8368cbc

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

.changeset/chilly-cobras-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'posthog-react-native': patch
3+
---
4+
5+
fix: posthog-cli finds the correct path on android gradle plugin

packages/react-native/tooling/posthog-xcode.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ fi
3232
# Check for posthog-cli using installer environment variables
3333
# TODO: provide a config that users can force the location
3434
# Xcode starts with a very limited $PATH so using whereis does not work
35-
if [ -n "$POSTHOG_CLI_INSTALL_DIR" ]; then
36-
PH_CLI_PATH="$POSTHOG_CLI_INSTALL_DIR/posthog-cli"
37-
elif [ -n "$CARGO_DIST_FORCE_INSTALL_DIR" ]; then
38-
PH_CLI_PATH="$CARGO_DIST_FORCE_INSTALL_DIR/posthog-cli"
39-
elif [ -f "$HOME/.posthog/posthog-cli" ]; then
35+
if [ -f "$HOME/.posthog/posthog-cli" ]; then
4036
PH_CLI_PATH="$HOME/.posthog/posthog-cli"
4137
else
4238
# Check if installed via npm -g @posthog/cli

packages/react-native/tooling/posthog.gradle

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
160221
def resolvePostHogReactNativeSDKPath(reactRoot) {
161222
def resolvedPath = null
162223
try {

0 commit comments

Comments
 (0)