Skip to content

Commit

Permalink
fix(windows): don't spawn separate process for autolink-windows (#1964
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tido64 committed Apr 15, 2024
1 parent a18a69a commit bb7e43d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
8 changes: 4 additions & 4 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
21 changes: 16 additions & 5 deletions windows/project.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as colors from "yoctocolors";
import {
findNearest,
getPackageVersion,
memo,
readJSONFile,
readTextFile,
requireTransitive,
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 26 additions & 13 deletions windows/test-app.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -324,17 +324,30 @@ 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 });
});
});
}

return undefined;
Expand Down

0 comments on commit bb7e43d

Please sign in to comment.