From 041522a19d7ab0d4ebd7e2fa52ef76463b9a8d6a Mon Sep 17 00:00:00 2001 From: youedd Date: Sat, 15 Apr 2023 13:24:03 +0200 Subject: [PATCH] Use node to resolve react-native package path --- src/commands/codepush/lib/react-native-utils.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/commands/codepush/lib/react-native-utils.ts b/src/commands/codepush/lib/react-native-utils.ts index 9fcf44876..cecf4f4ad 100644 --- a/src/commands/codepush/lib/react-native-utils.ts +++ b/src/commands/codepush/lib/react-native-utils.ts @@ -12,6 +12,7 @@ const plist = require("plist"); const g2js = require("gradle-to-js/lib/parser"); const properties = require("properties"); import * as childProcess from "child_process"; +import { directoryExistsSync } from "../../../util/misc/fs-helper"; export interface VersionSearchParams { os: string; // ios or android @@ -541,7 +542,7 @@ async function getHermesCommand(gradleFile: string): Promise { } }; // Hermes is bundled with react-native since 0.69 - const bundledHermesEngine = path.join("node_modules", "react-native", "sdks", "hermesc", getHermesOSBin(), getHermesOSExe()); + const bundledHermesEngine = path.join(getReactNativePackagePath(), "sdks", "hermesc", getHermesOSBin(), getHermesOSExe()); if (fileExists(bundledHermesEngine)) { return bundledHermesEngine; } @@ -561,7 +562,7 @@ async function getHermesCommand(gradleFile: string): Promise { function getComposeSourceMapsPath(): string { // detect if compose-source-maps.js script exists - const composeSourceMaps = path.join("node_modules", "react-native", "scripts", "compose-source-maps.js"); + const composeSourceMaps = path.join(getReactNativePackagePath(), "scripts", "compose-source-maps.js"); if (fs.existsSync(composeSourceMaps)) { return composeSourceMaps; } @@ -570,12 +571,22 @@ function getComposeSourceMapsPath(): string { function getCliPath(): string { if (process.platform === "win32") { - return path.join("node_modules", "react-native", "local-cli", "cli.js"); + return path.join(getReactNativePackagePath(), "local-cli", "cli.js"); } return path.join("node_modules", ".bin", "react-native"); } +function getReactNativePackagePath(): string { + const result = childProcess.spawnSync("node", ["--print", "require.resolve('react-native/package.json')"]); + const packagePath = path.dirname(result.stdout.toString()); + if (directoryExistsSync(packagePath)) { + return packagePath; + } + + return path.join("node_modules", "react-native"); +} + export function isValidOS(os: string): boolean { switch (os.toLowerCase()) { case "android":