target-platform.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import process from "node:process";
  2. function normalizeTargetOs(rawOs) {
  3. const value = (rawOs ?? "").toLowerCase();
  4. switch (value) {
  5. case "mac":
  6. case "macos":
  7. case "darwin":
  8. case "osx":
  9. return "darwin";
  10. case "win":
  11. case "windows":
  12. case "win32":
  13. return "win32";
  14. case "linux":
  15. return "linux";
  16. default:
  17. throw new Error(`Unsupported target OS: ${rawOs}`);
  18. }
  19. }
  20. function normalizeTargetArch(rawArch) {
  21. const value = (rawArch ?? "").toLowerCase();
  22. switch (value) {
  23. case "x64":
  24. case "amd64":
  25. case "x86_64":
  26. return "x64";
  27. case "arm64":
  28. case "aarch64":
  29. return "arm64";
  30. default:
  31. throw new Error(`Unsupported target arch: ${rawArch}`);
  32. }
  33. }
  34. export function getTargetPlatform() {
  35. const targetOs = process.env.ZCODE_TARGET_OS ?? process.platform;
  36. const targetArch = process.env.ZCODE_TARGET_ARCH ?? process.arch;
  37. const os = normalizeTargetOs(targetOs);
  38. const arch = normalizeTargetArch(targetArch);
  39. return {
  40. os,
  41. arch,
  42. key: `${os}-${arch}`,
  43. npmOs: os,
  44. npmCpu: arch,
  45. // 部分 Linux optional native 包声明了 libc=glibc。
  46. // 跨平台 prepare 时如果只传 --os/--cpu,npm 仍会按宿主机 libc 判定为不匹配,导致补装失败。
  47. npmLibc: os === "linux" ? "glibc" : undefined,
  48. };
  49. }
  50. export function resolvePlatformKeyForPackagedApp() {
  51. return `${process.platform}-${process.arch}`;
  52. }