46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
import { execSync } from "node:child_process";
|
|
|
|
const isWindows = process.platform === "win32";
|
|
|
|
export type PlatformArch = "x64" | "arm64";
|
|
export type PlatformDir = "linux-x64" | "linux-arm64" | "macos-x64" | "macos-arm64" | "windows-x64";
|
|
|
|
export function getBinaryName(base: string): string {
|
|
return isWindows ? `${base}.exe` : base;
|
|
}
|
|
|
|
function getEffectiveArch(): PlatformArch {
|
|
const platform = process.platform;
|
|
const nodeArch = process.arch;
|
|
if (platform === "darwin") {
|
|
if (nodeArch === "arm64") return "arm64";
|
|
try {
|
|
const translated = execSync("sysctl -in sysctl.proc_translated", {
|
|
encoding: "utf8",
|
|
}).trim();
|
|
if (translated === "1") return "arm64";
|
|
} catch {}
|
|
return "x64";
|
|
}
|
|
if (/arm/i.test(nodeArch)) return "arm64";
|
|
if (platform === "win32") {
|
|
const pa = process.env.PROCESSOR_ARCHITECTURE || "";
|
|
const paw = process.env.PROCESSOR_ARCHITEW6432 || "";
|
|
if (/arm/i.test(pa) || /arm/i.test(paw)) return "arm64";
|
|
}
|
|
return "x64";
|
|
}
|
|
|
|
export function getPlatformDir(): PlatformDir {
|
|
const platform = process.platform;
|
|
const arch = getEffectiveArch();
|
|
if (platform === "linux" && arch === "x64") return "linux-x64";
|
|
if (platform === "linux" && arch === "arm64") return "linux-arm64";
|
|
if (platform === "darwin" && arch === "x64") return "macos-x64";
|
|
if (platform === "darwin" && arch === "arm64") return "macos-arm64";
|
|
if (platform === "win32" && arch === "x64") return "windows-x64";
|
|
// Windows ARM64 runs x64 binaries via emulation — no native arm64 build yet
|
|
if (platform === "win32" && arch === "arm64") return "windows-x64";
|
|
throw new Error(`Unsupported platform: ${platform}-${arch}`);
|
|
}
|