fix(windows): don't spawn separate process for `autolink-windows` (#1964)

This commit is contained in:
Tommy Nguyen 2024-04-15 18:37:25 +02:00 коммит произвёл GitHub
Родитель a18a69a7fc
Коммит bb7e43d5bb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 46 добавлений и 22 удалений

Просмотреть файл

@ -76,15 +76,15 @@ function isMain(url, script = process.argv[1]) {
/**
* @template T
* @param {() => T} fn
* @returns {() => T}
* @param {(...args: any[]) => T} fn
* @returns {(...args: any[]) => T}
*/
function memo(fn) {
/** @type {T} */
let result;
return () => {
return (...args) => {
if (result === undefined) {
result = fn();
result = fn(...args);
}
return result;
};

Просмотреть файл

@ -7,6 +7,7 @@ import * as colors from "yoctocolors";
import {
findNearest,
getPackageVersion,
memo,
readJSONFile,
readTextFile,
requireTransitive,
@ -90,6 +91,19 @@ function generateCertificateItems(
return items.join("\n ");
}
/**
* Equivalent to invoking `react-native config`.
* @param {string} rnWindowsPath
*/
export const loadReactNativeConfig = memo((rnWindowsPath) => {
/** @type {import("@react-native-community/cli")} */
const { loadConfig } = requireTransitive(
["@react-native-community/cli"],
rnWindowsPath
);
return loadConfig();
});
/**
* @param {string} message
*/
@ -196,12 +210,9 @@ function getNuGetDependencies(rnWindowsPath, fs = nodefs) {
return [];
}
/** @type {import("@react-native-community/cli")} */
const { loadConfig } = requireTransitive(
["@react-native-community/cli"],
rnWindowsPath
const dependencies = Object.values(
loadReactNativeConfig(rnWindowsPath).dependencies
);
const dependencies = Object.values(loadConfig().dependencies);
const xml = new XMLParser({
ignoreAttributes: false,

Просмотреть файл

@ -1,6 +1,5 @@
#!/usr/bin/env node
// @ts-check
import { spawn } from "node:child_process";
import * as nodefs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
@ -16,7 +15,7 @@ import {
} from "../scripts/helpers.js";
import { parseArgs } from "../scripts/parseargs.mjs";
import { validate } from "../scripts/validate-manifest.js";
import { projectInfo } from "./project.mjs";
import { loadReactNativeConfig, projectInfo } from "./project.mjs";
import { configureForUWP } from "./uwp.mjs";
import { configureForWin32 } from "./win32.mjs";
@ -193,11 +192,12 @@ export function generateSolution(destPath, options, fs = nodefs) {
["@react-native-windows/cli", "mustache"],
rnWindowsPath
);
const slnPath = path.join(destPath, `${info.bundle.appName}.sln`);
const vcxprojPath = path.join(projectFilesDestPath, projectFileName);
const vcxprojLocalPath = path.relative(destPath, vcxprojPath);
copyTasks.push(
writeTextFile(
path.join(destPath, `${info.bundle.appName}.sln`),
slnPath,
mustache
.render(readTextFile(solutionTemplate, fs), {
...templateView,
@ -324,16 +324,29 @@ export function generateSolution(destPath, options, fs = nodefs) {
}
if (options.autolink) {
Promise.all(copyTasks).then(() => {
spawn(
path.join(path.dirname(process.argv0), "npx.cmd"),
["react-native", "autolink-windows", "--proj", vcxprojPath],
{ stdio: "inherit" }
).on("close", (code) => {
if (code !== 0) {
process.exitCode = code || 1;
const projectRoot = path.resolve(path.dirname(projectManifest));
Promise.all(copyTasks)
.then(() => {
// `react-native config` is cached by `@react-native-community/cli`. We
// need to manually regenerate the Windows project config and inject it.
const config = loadReactNativeConfig(rnWindowsPath);
config.project.windows = config.platforms.windows.projectConfig(
projectRoot,
{
sourceDir: path.relative(projectRoot, destPath),
solutionFile: path.relative(destPath, slnPath),
project: {
projectFile: vcxprojLocalPath,
},
}
});
);
return config;
})
.then((config) => {
const autolink = config.commands.find(
({ name }) => name === "autolink-windows"
);
autolink?.func([], config, { proj: vcxprojPath });
});
}