Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-cobras-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-react-native': patch
---

fix: posthog-cli finds the correct path on android gradle plugin
6 changes: 1 addition & 5 deletions packages/react-native/tooling/posthog-xcode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ fi
# Check for posthog-cli using installer environment variables
# TODO: provide a config that users can force the location
# Xcode starts with a very limited $PATH so using whereis does not work
if [ -n "$POSTHOG_CLI_INSTALL_DIR" ]; then
PH_CLI_PATH="$POSTHOG_CLI_INSTALL_DIR/posthog-cli"
elif [ -n "$CARGO_DIST_FORCE_INSTALL_DIR" ]; then
PH_CLI_PATH="$CARGO_DIST_FORCE_INSTALL_DIR/posthog-cli"
Comment on lines -35 to -38
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those were never used so lets remove them and keep synced with the others

elif [ -f "$HOME/.posthog/posthog-cli" ]; then
if [ -f "$HOME/.posthog/posthog-cli" ]; then
PH_CLI_PATH="$HOME/.posthog/posthog-cli"
else
# Check if installed via npm -g @posthog/cli
Expand Down
65 changes: 63 additions & 2 deletions packages/react-native/tooling/posthog.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ plugins.withId('com.android.application') {
injected.execOps.exec {
workingDir reactRoot

def cliPackage = "posthog-cli"
def cliPackage = resolvePostHogCliPackagePath(reactRoot)
def args = [cliPackage]

args.addAll(["exp", "hermes", "clone",
Expand All @@ -103,7 +103,7 @@ plugins.withId('com.android.application') {
injected.execOps.exec {
workingDir reactRoot

def cliPackage = "posthog-cli"
def cliPackage = resolvePostHogCliPackagePath(reactRoot)
def args = [cliPackage]

def sourcemapDir = sourcemapOutput.getParent()
Expand Down Expand Up @@ -157,6 +157,67 @@ plugins.withId('com.android.application') {
}
}

def resolvePostHogCliPackagePath(reactRoot) {
// First, try to find @posthog/cli folder from require.resolve
try {
def resolvedPath = new File(["node", "--print", "require.resolve('@posthog/cli/package.json')"].execute(null, rootDir).text.trim()).getParentFile()
if (resolvedPath != null && resolvedPath.exists()) {
def runPostHogCliFile = new File(resolvedPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli dynamically: `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
} catch (Throwable ignored) {}

// Second, check if @posthog/cli exists in $reactRoot/node_modules
def nodeModulesPath = new File("$reactRoot/node_modules/@posthog/cli")
if (nodeModulesPath.exists()) {
def runPostHogCliFile = new File(nodeModulesPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli hard-coded path (yarn or npm): `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}

// Third, check for pnpm installation with node_modules/.bin/posthog-cli
def pnpmBinPath = new File("$reactRoot/node_modules/.bin/posthog-cli")
if (pnpmBinPath.exists()) {
project.logger.info("resolved posthog-cli hard-coded path (pnpm): `${pnpmBinPath.getAbsolutePath()}`")
return pnpmBinPath.getAbsolutePath()
}

// Fourth, check using npm root for local installation
try {
def npmRoot = ["npm", "root"].execute(null, rootDir).text.trim()
def npmPostHogCliPath = new File(npmRoot, "@posthog/cli")
if (npmPostHogCliPath.exists()) {
def runPostHogCliFile = new File(npmPostHogCliPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli via npm root: `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
} catch (Throwable ignored) {}

// Fifth, check for global npm installation of @posthog/cli
try {
def globalPrefix = ["npm", "prefix", "-g"].execute().text.trim()
def globalPostHogCliPath = new File(globalPrefix, "lib/node_modules/@posthog/cli")
if (globalPostHogCliPath.exists()) {
def runPostHogCliFile = new File(globalPostHogCliPath, "run-posthog-cli.js")
if (runPostHogCliFile.exists()) {
project.logger.info("resolved posthog-cli from global npm installation: `${runPostHogCliFile.getAbsolutePath()}`")
return runPostHogCliFile.getAbsolutePath()
}
}
} catch (Throwable ignored) {}

// Finally, fallback to global install
project.logger.info("falling back to global posthog-cli")
return "posthog-cli"
}

def resolvePostHogReactNativeSDKPath(reactRoot) {
def resolvedPath = null
try {
Expand Down
Loading