Skip to content

Commit bb7e43d

Browse files
authored
fix(windows): don't spawn separate process for autolink-windows (#1964)
1 parent a18a69a commit bb7e43d

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

scripts/helpers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ function isMain(url, script = process.argv[1]) {
7676

7777
/**
7878
* @template T
79-
* @param {() => T} fn
80-
* @returns {() => T}
79+
* @param {(...args: any[]) => T} fn
80+
* @returns {(...args: any[]) => T}
8181
*/
8282
function memo(fn) {
8383
/** @type {T} */
8484
let result;
85-
return () => {
85+
return (...args) => {
8686
if (result === undefined) {
87-
result = fn();
87+
result = fn(...args);
8888
}
8989
return result;
9090
};

windows/project.mjs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as colors from "yoctocolors";
77
import {
88
findNearest,
99
getPackageVersion,
10+
memo,
1011
readJSONFile,
1112
readTextFile,
1213
requireTransitive,
@@ -90,6 +91,19 @@ function generateCertificateItems(
9091
return items.join("\n ");
9192
}
9293

94+
/**
95+
* Equivalent to invoking `react-native config`.
96+
* @param {string} rnWindowsPath
97+
*/
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+
return loadConfig();
105+
});
106+
93107
/**
94108
* @param {string} message
95109
*/
@@ -196,12 +210,9 @@ function getNuGetDependencies(rnWindowsPath, fs = nodefs) {
196210
return [];
197211
}
198212

199-
/** @type {import("@react-native-community/cli")} */
200-
const { loadConfig } = requireTransitive(
201-
["@react-native-community/cli"],
202-
rnWindowsPath
213+
const dependencies = Object.values(
214+
loadReactNativeConfig(rnWindowsPath).dependencies
203215
);
204-
const dependencies = Object.values(loadConfig().dependencies);
205216

206217
const xml = new XMLParser({
207218
ignoreAttributes: false,

windows/test-app.mjs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
22
// @ts-check
3-
import { spawn } from "node:child_process";
43
import * as nodefs from "node:fs";
54
import * as os from "node:os";
65
import * as path from "node:path";
@@ -16,7 +15,7 @@ import {
1615
} from "../scripts/helpers.js";
1716
import { parseArgs } from "../scripts/parseargs.mjs";
1817
import { validate } from "../scripts/validate-manifest.js";
19-
import { projectInfo } from "./project.mjs";
18+
import { loadReactNativeConfig, projectInfo } from "./project.mjs";
2019
import { configureForUWP } from "./uwp.mjs";
2120
import { 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

Comments
 (0)