Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

build-target-arch.ts

55 lines2.0 KB

1import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
2import * as Config from "effect/Config";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5​
6export type BuildArch = "arm64" | "x64" | "universal";
7export type BuildPlatform = "mac" | "linux" | "win";
8​
9interface PlatformConfig {
10 readonly archChoices: ReadonlyArray<BuildArch>;
12​
13const WindowsProcessorArchitectureConfig = Config.all({
14 processorArchitecture: Config.string("PROCESSOR_ARCHITECTURE").pipe(Config.option),
15 processorArchitectureW6432: Config.string("PROCESSOR_ARCHITEW6432").pipe(Config.option),
16});
17​
18function normalizeWindowsArch(value: string | undefined): BuildArch | undefined {
19 const normalized = value?.trim().toLowerCase();
20 if (!normalized) return undefined;
21 if (normalized.includes("arm64") || normalized === "aarch64") return "arm64";
22 if (normalized.includes("amd64") || normalized.includes("x64")) return "x64";
23 return undefined;
25​
26const resolveHostProcessArch = Effect.fn("resolveHostProcessArch")(function* () {
27 const platform = yield* HostProcessPlatform;
28 const processArch = yield* HostProcessArchitecture;
29 if (processArch === "arm64") return "arm64";
30 if (processArch === "x64") {
31 if (platform !== "win32") return "x64";
32​
33 // On Windows-on-Arm, x64 Node/Bun can run under emulation while the host
34 // still reports ARM64 via the processor environment variables.
35 const env = yield* WindowsProcessorArchitectureConfig;
36 return (
37 normalizeWindowsArch(Option.getOrUndefined(env.processorArchitectureW6432)) ??
38 normalizeWindowsArch(Option.getOrUndefined(env.processorArchitecture)) ??
39 "x64"
40 );
41 }
42 return undefined;
43});
44​
45export const getDefaultBuildArch = Effect.fn("getDefaultBuildArch")(function* (
46 platform: BuildPlatform,
47 platformConfig: PlatformConfig,
48) {
49 const hostArch = yield* resolveHostProcessArch();
50 if (hostArch && platformConfig.archChoices.includes(hostArch)) {
51 return hostArch;
52 }
53​
54 return platformConfig.archChoices[0] ?? "x64";
55});