11// @ts -check
2+ import { resolveCommunityCLI } from "@rnx-kit/tools-react-native/context" ;
23import { XMLParser } from "fast-xml-parser" ;
34import * as nodefs from "node:fs" ;
45import * as path from "node:path" ;
@@ -10,19 +11,21 @@ import {
1011 memo ,
1112 readJSONFile ,
1213 readTextFile ,
13- requireTransitive ,
1414 toVersionNumber ,
1515 v ,
1616} from "../scripts/helpers.js" ;
1717import * as colors from "../scripts/utils/colors.mjs" ;
1818
1919/**
20- * @typedef {import("../scripts/types.js").AppManifest } AppManifest
21- * @typedef {import("../scripts/types.js").AppxBundle } AppxBundle
22- * @typedef {import("../scripts/types.js").AssetItems } AssetItems;
23- * @typedef {import("../scripts/types.js").Assets } Assets;
24- * @typedef {import("../scripts/types.js").MSBuildProjectOptions } MSBuildProjectOptions;
25- * @typedef {import("../scripts/types.js").ProjectInfo } ProjectInfo;
20+ * @import { Config } from "@react-native-community/cli-types"
21+ * @import {
22+ * AppManifest,
23+ * AppxBundle,
24+ * AssetItems,
25+ * Assets,
26+ * MSBuildProjectOptions,
27+ * ProjectInfo,
28+ * } from "../scripts/types.js"
2629 */
2730
2831const uniqueFilterIdentifier = "e48dc53e-40b1-40cb-970a-f89935452892" ;
@@ -93,18 +96,22 @@ function generateCertificateItems(
9396
9497/**
9598 * Equivalent to invoking `react-native config`.
96- * @param { string } rnWindowsPath
99+ * @type { (projectRoot: string) => Promise<Config> }
97100 */
98- export const loadReactNativeConfig = memo ( ( rnWindowsPath ) => {
99- /** @type {import("@react-native-community/cli") } */
100- const { loadConfig } = requireTransitive (
101- [ "@react-native-community/cli" ] ,
102- rnWindowsPath
103- ) ;
104- // The signature of `loadConfig` changed in 14.0.0:
105- // https://github.com/react-native-community/cli/commit/b787c89edb781bb788576cd615d2974fc81402fc
106- // @ts -expect-error TS2345: Argument of type X is not assignable to parameter of type Y
107- return loadConfig . length === 1 ? loadConfig ( { } ) : loadConfig ( ) ;
101+ export const loadReactNativeConfig = memo ( ( projectRoot ) => {
102+ const rncli = resolveCommunityCLI ( projectRoot ) ;
103+ return import ( rncli ) . then ( async ( cli ) => {
104+ const { loadConfig, loadConfigAsync } = cli ?. default ?? cli ;
105+ if ( ! loadConfigAsync ) {
106+ // The signature of `loadConfig` changed in 14.0.0:
107+ // https://github.com/react-native-community/cli/commit/b787c89edb781bb788576cd615d2974fc81402fc
108+ return loadConfig . length === 1
109+ ? loadConfig ( { projectRoot } )
110+ : loadConfig ( ) ;
111+ }
112+
113+ return await loadConfigAsync ( { projectRoot } ) ;
114+ } ) ;
108115} ) ;
109116
110117/**
@@ -204,18 +211,17 @@ function generateContentItems(
204211 * is a workaround until `react-native-windows` autolinking adds support.
205212 *
206213 * @see {@link https://github.com/microsoft/react-native-windows/issues/9578 }
207- * @param {string } rnWindowsPath
208- * @returns {[string, string][] }
214+ * @param {string } projectRoot
215+ * @returns {Promise< [string, string][]> }
209216 */
210- function getNuGetDependencies ( rnWindowsPath , fs = nodefs ) {
217+ async function getNuGetDependencies ( projectRoot , fs = nodefs ) {
211218 const pkgJson = findNearest ( "package.json" , undefined , fs ) ;
212219 if ( ! pkgJson ) {
213220 return [ ] ;
214221 }
215222
216- const dependencies = Object . values (
217- loadReactNativeConfig ( rnWindowsPath ) . dependencies
218- ) ;
223+ const config = await loadReactNativeConfig ( projectRoot ) ;
224+ const dependencies = Object . values ( config . dependencies ) ;
219225
220226 const xml = new XMLParser ( {
221227 ignoreAttributes : false ,
@@ -400,9 +406,9 @@ export function getBundleResources(manifestFilePath, fs = nodefs) {
400406 * @param {MSBuildProjectOptions } options
401407 * @param {string } rnWindowsPath
402408 * @param {string } destPath
403- * @returns {ProjectInfo }
409+ * @returns {Promise< ProjectInfo> }
404410 */
405- export function projectInfo (
411+ export async function projectInfo (
406412 { useFabric, useNuGet } ,
407413 rnWindowsPath ,
408414 destPath ,
@@ -421,7 +427,7 @@ export function projectInfo(
421427 version,
422428 versionNumber,
423429 bundle : getBundleResources ( findNearest ( "app.json" , destPath , fs ) , fs ) ,
424- nugetDependencies : getNuGetDependencies ( rnWindowsPath ) ,
430+ nugetDependencies : await getNuGetDependencies ( destPath ) ,
425431 useExperimentalNuGet : newArch || useNuGet ,
426432 useFabric : newArch ,
427433 } ;
0 commit comments