|
| 1 | +import type { Command, Config } from "@react-native-community/cli-types"; |
| 2 | +import { invalidateState } from "@rnx-kit/tools-react-native/cache"; |
| 3 | +import * as fs from "node:fs"; |
| 4 | +import * as os from "node:os"; |
| 5 | +import * as path from "node:path"; |
| 6 | +import ora from "ora"; |
| 7 | +import type { WindowsBuildParams, WindowsInputParams } from "./types.ts"; |
| 8 | + |
| 9 | +export type BuildArgs = { |
| 10 | + solution: string; |
| 11 | + args: string[]; |
| 12 | +}; |
| 13 | + |
| 14 | +export type BuildResult = BuildArgs | number | null; |
| 15 | + |
| 16 | +function findRunCommand(startDir: string): Command | undefined { |
| 17 | + try { |
| 18 | + const fromProjectRoot = { paths: [startDir] }; |
| 19 | + const rnwPath = require.resolve( |
| 20 | + "react-native-windows/package.json", |
| 21 | + fromProjectRoot |
| 22 | + ); |
| 23 | + |
| 24 | + const fromRnwDir = { paths: [path.dirname(rnwPath)] }; |
| 25 | + const cliPath = require.resolve("@react-native-windows/cli", fromRnwDir); |
| 26 | + |
| 27 | + const cli = require(cliPath) as typeof import("@react-native-windows/cli"); |
| 28 | + return cli.commands.find((cmd) => cmd.name === "run-windows"); |
| 29 | + } catch (_) { |
| 30 | + // Handled by caller |
| 31 | + } |
| 32 | + |
| 33 | + return undefined; |
| 34 | +} |
| 35 | + |
| 36 | +function findSolution(searchDir: string, logger: ora.Ora): string | undefined { |
| 37 | + const solutions = fs.existsSync(searchDir) |
| 38 | + ? fs.readdirSync(searchDir).filter((file) => file.endsWith(".sln")) |
| 39 | + : []; |
| 40 | + |
| 41 | + if (solutions.length === 0) { |
| 42 | + invalidateState(); |
| 43 | + process.exitCode = 1; |
| 44 | + logger.fail( |
| 45 | + "No Visual Studio solutions were found; specify a Visual Studio solution with `--solution`" |
| 46 | + ); |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + |
| 50 | + if (solutions.length > 1) { |
| 51 | + logger.info( |
| 52 | + `Multiple Visual Studio solutions were found; picking the first one: ${solutions.join(", ")}` |
| 53 | + ); |
| 54 | + logger.info("If this is wrong, specify another solution with `--solution`"); |
| 55 | + } |
| 56 | + |
| 57 | + return path.join(searchDir, solutions[0]); |
| 58 | +} |
| 59 | + |
| 60 | +function toRunWindowsOptions( |
| 61 | + sln: string, |
| 62 | + { root }: Config, |
| 63 | + { configuration, architecture, launch, deploy }: WindowsBuildParams |
| 64 | +) { |
| 65 | + return { |
| 66 | + release: configuration === "Release", |
| 67 | + root, |
| 68 | + arch: architecture ?? os.arch(), |
| 69 | + packager: false, |
| 70 | + bundle: false, |
| 71 | + launch: Boolean(launch), |
| 72 | + autolink: true, |
| 73 | + build: true, |
| 74 | + deploy: Boolean(deploy), |
| 75 | + sln, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +export function runWindowsCommand( |
| 80 | + config: Config, |
| 81 | + params: WindowsInputParams, |
| 82 | + logger: ora.Ora, |
| 83 | + callback: ( |
| 84 | + solution: string, |
| 85 | + run: Command["func"], |
| 86 | + options: Record<string, unknown> |
| 87 | + ) => Promise<BuildResult> |
| 88 | +) { |
| 89 | + if (process.platform !== "win32") { |
| 90 | + logger.fail("Windows builds can only be performed on Windows hosts"); |
| 91 | + return Promise.resolve(1); |
| 92 | + } |
| 93 | + |
| 94 | + const sourceDir = "windows"; |
| 95 | + const solution = params.solution || findSolution(sourceDir, logger); |
| 96 | + if (!solution) { |
| 97 | + return Promise.resolve(1); |
| 98 | + } |
| 99 | + |
| 100 | + const runCommand = findRunCommand(config.root); |
| 101 | + if (!runCommand) { |
| 102 | + logger.fail( |
| 103 | + "Failed to find `@react-native-windows/cli`, make sure `react-native-windows` is installed." |
| 104 | + ); |
| 105 | + return Promise.resolve(1); |
| 106 | + } |
| 107 | + |
| 108 | + const options = toRunWindowsOptions(solution, config, params); |
| 109 | + return callback(solution, runCommand.func, options); |
| 110 | +} |
| 111 | + |
| 112 | +export function buildWindows( |
| 113 | + config: Config, |
| 114 | + params: WindowsInputParams, |
| 115 | + additionalArgs: string[], |
| 116 | + logger = ora() |
| 117 | +): Promise<BuildResult> { |
| 118 | + return runWindowsCommand(config, params, logger, (solution, run, options) => { |
| 119 | + const build = run(additionalArgs, config, options); |
| 120 | + return build |
| 121 | + ? build.then(() => ({ solution, args: additionalArgs })) |
| 122 | + : Promise.resolve(1); |
| 123 | + }); |
| 124 | +} |
0 commit comments