|
| 1 | +import type { 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 path from "node:path"; |
| 5 | +import ora from "ora"; |
| 6 | +import type { WindowsBuildParams, WindowsInputParams } from "./types"; |
| 7 | +import { watch } from "./watcher"; |
| 8 | + |
| 9 | +export type BuildArgs = { |
| 10 | + solution: string; |
| 11 | + args: string[]; |
| 12 | +}; |
| 13 | + |
| 14 | +export type BuildResult = BuildArgs | number | null; |
| 15 | + |
| 16 | +function findSolution(searchDir: string, logger: ora.Ora): string | undefined { |
| 17 | + const solutions = fs.existsSync(searchDir) |
| 18 | + ? fs.readdirSync(searchDir).filter((file) => file.endsWith(".sln")) |
| 19 | + : []; |
| 20 | + |
| 21 | + if (solutions.length === 0) { |
| 22 | + invalidateState(); |
| 23 | + process.exitCode = 1; |
| 24 | + logger.fail( |
| 25 | + "No Visual Studio solutions were found; specify a Visual Studio solution with `--solution`" |
| 26 | + ); |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + |
| 30 | + if (solutions.length > 1) { |
| 31 | + logger.fail( |
| 32 | + `Multiple Visual Studio solutions were found; picking the first one: ${solutions.join(", ")}` |
| 33 | + ); |
| 34 | + logger.fail("If this is wrong, specify another solution with `--solution`"); |
| 35 | + } |
| 36 | + |
| 37 | + return path.join(searchDir, solutions[0]); |
| 38 | +} |
| 39 | + |
| 40 | +function runBuild( |
| 41 | + solution: string, |
| 42 | + buildParams: WindowsBuildParams, |
| 43 | + additionalArgs: string[], |
| 44 | + logger: ora.Ora |
| 45 | +): Promise<BuildResult> { |
| 46 | + return import("@rnx-kit/tools-windows").then(({ buildAppxBundle }) => { |
| 47 | + const build = buildAppxBundle(solution, buildParams, additionalArgs); |
| 48 | + return watch(build, logger, () => ({ |
| 49 | + solution, |
| 50 | + args: build.spawnargs, |
| 51 | + })); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +export function buildWindows( |
| 56 | + _config: Config, |
| 57 | + { solution, ...buildParams }: WindowsInputParams, |
| 58 | + additionalArgs: string[], |
| 59 | + logger = ora() |
| 60 | +): Promise<BuildResult> { |
| 61 | + if (process.platform !== "win32") { |
| 62 | + logger.fail("Windows builds can only be performed on Windows hosts"); |
| 63 | + return Promise.resolve(1); |
| 64 | + } |
| 65 | + |
| 66 | + const sourceDir = "windows"; |
| 67 | + const sln = solution || findSolution(sourceDir, logger); |
| 68 | + if (!sln) { |
| 69 | + return Promise.resolve(1); |
| 70 | + } |
| 71 | + |
| 72 | + return runBuild(sln, buildParams, additionalArgs, logger); |
| 73 | +} |
0 commit comments