11#!/usr/bin/env node
22// @ts -check
3- import { spawn } from "node:child_process" ;
43import * as nodefs from "node:fs" ;
54import * as os from "node:os" ;
65import * as path from "node:path" ;
@@ -16,7 +15,7 @@ import {
1615} from "../scripts/helpers.js" ;
1716import { parseArgs } from "../scripts/parseargs.mjs" ;
1817import { validate } from "../scripts/validate-manifest.js" ;
19- import { projectInfo } from "./project.mjs" ;
18+ import { loadReactNativeConfig , projectInfo } from "./project.mjs" ;
2019import { configureForUWP } from "./uwp.mjs" ;
2120import { configureForWin32 } from "./win32.mjs" ;
2221
@@ -193,11 +192,12 @@ export function generateSolution(destPath, options, fs = nodefs) {
193192 [ "@react-native-windows/cli" , "mustache" ] ,
194193 rnWindowsPath
195194 ) ;
195+ const slnPath = path . join ( destPath , `${ info . bundle . appName } .sln` ) ;
196196 const vcxprojPath = path . join ( projectFilesDestPath , projectFileName ) ;
197197 const vcxprojLocalPath = path . relative ( destPath , vcxprojPath ) ;
198198 copyTasks . push (
199199 writeTextFile (
200- path . join ( destPath , ` ${ info . bundle . appName } .sln` ) ,
200+ slnPath ,
201201 mustache
202202 . render ( readTextFile ( solutionTemplate , fs ) , {
203203 ...templateView ,
@@ -324,17 +324,30 @@ export function generateSolution(destPath, options, fs = nodefs) {
324324 }
325325
326326 if ( options . autolink ) {
327- Promise . all ( copyTasks ) . then ( ( ) => {
328- spawn (
329- path . join ( path . dirname ( process . argv0 ) , "npx.cmd" ) ,
330- [ "react-native" , "autolink-windows" , "--proj" , vcxprojPath ] ,
331- { stdio : "inherit" }
332- ) . on ( "close" , ( code ) => {
333- if ( code !== 0 ) {
334- process . exitCode = code || 1 ;
335- }
327+ const projectRoot = path . resolve ( path . dirname ( projectManifest ) ) ;
328+ Promise . all ( copyTasks )
329+ . then ( ( ) => {
330+ // `react-native config` is cached by `@react-native-community/cli`. We
331+ // need to manually regenerate the Windows project config and inject it.
332+ const config = loadReactNativeConfig ( rnWindowsPath ) ;
333+ config . project . windows = config . platforms . windows . projectConfig (
334+ projectRoot ,
335+ {
336+ sourceDir : path . relative ( projectRoot , destPath ) ,
337+ solutionFile : path . relative ( destPath , slnPath ) ,
338+ project : {
339+ projectFile : vcxprojLocalPath ,
340+ } ,
341+ }
342+ ) ;
343+ return config ;
344+ } )
345+ . then ( ( config ) => {
346+ const autolink = config . commands . find (
347+ ( { name } ) => name === "autolink-windows"
348+ ) ;
349+ autolink ?. func ( [ ] , config , { proj : vcxprojPath } ) ;
336350 } ) ;
337- } ) ;
338351 }
339352
340353 return undefined ;
0 commit comments