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