diff --git a/.changeset/chilly-cobras-brush.md b/.changeset/chilly-cobras-brush.md new file mode 100644 index 0000000000..787c657dc2 --- /dev/null +++ b/.changeset/chilly-cobras-brush.md @@ -0,0 +1,5 @@ +--- +'posthog-react-native': patch +--- + +fix: posthog-cli finds the correct path on android gradle plugin diff --git a/packages/react-native/tooling/posthog-xcode.sh b/packages/react-native/tooling/posthog-xcode.sh index 102fc28d64..ef8db5fc5f 100755 --- a/packages/react-native/tooling/posthog-xcode.sh +++ b/packages/react-native/tooling/posthog-xcode.sh @@ -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" -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 diff --git a/packages/react-native/tooling/posthog.gradle b/packages/react-native/tooling/posthog.gradle index 5313c6cb08..82d7a9ad95 100644 --- a/packages/react-native/tooling/posthog.gradle +++ b/packages/react-native/tooling/posthog.gradle @@ -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", @@ -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() @@ -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 {