desktop-build-paths.mjs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /** Resolve build-owned Desktop paths without sharing mutable state across release targets. */
  2. import { join, resolve } from 'node:path'
  3. const APP_ROOT = resolve(import.meta.dirname, '..')
  4. const BUILD_ROOT = join(APP_ROOT, '.desktop-build')
  5. const SUPPORTED_TARGETS = new Set(['mac-arm64', 'mac-x64', 'win-x64'])
  6. /**
  7. * Resolve the fixed build target selected by a packaging environment.
  8. * @param {NodeJS.ProcessEnv} env - Packaging environment.
  9. * @param {NodeJS.Platform} hostPlatform - Build-host platform used when no target override exists.
  10. * @param {string} hostArch - Build-host architecture used when no target override exists.
  11. * @returns {'mac-arm64' | 'mac-x64' | 'win-x64'} Supported Desktop target name.
  12. */
  13. export function resolveDesktopBuildTarget(
  14. env = process.env,
  15. hostPlatform = process.platform,
  16. hostArch = process.arch,
  17. ) {
  18. const platform = env.DSH_DESKTOP_TARGET_PLATFORM ?? env.npm_config_platform ?? hostPlatform
  19. const arch = env.DSH_DESKTOP_TARGET_ARCH ?? env.npm_config_arch
  20. ?? (platform === 'win32' || platform === 'win' ? 'x64' : hostArch)
  21. const os = platform === 'darwin' ? 'mac' : platform === 'win32' || platform === 'win' ? 'win' : platform
  22. const target = `${os}-${arch}`
  23. if (!SUPPORTED_TARGETS.has(target)) {
  24. throw new Error(`desktop build paths: unsupported target ${target}`)
  25. }
  26. return /** @type {'mac-arm64' | 'mac-x64' | 'win-x64'} */ (target)
  27. }
  28. /**
  29. * Return the mutable preparation and artifact directories owned by one release target.
  30. * @param {'mac-arm64' | 'mac-x64' | 'win-x64'} target - Supported Desktop target name.
  31. * @returns {{ root: string, artifacts: string, runtime: string, packageSet: string, dsh: string, dshPnpm: string, electron: string, packedDsh: string, packedVendor: string, packedLandlock: string, downloads: string }} Target paths plus the shared immutable download cache.
  32. */
  33. export function desktopTargetBuildPaths(target) {
  34. if (!SUPPORTED_TARGETS.has(target)) {
  35. throw new Error(`desktop build paths: unsupported target ${String(target)}`)
  36. }
  37. const root = join(BUILD_ROOT, 'targets', target)
  38. const packed = join(root, 'packed')
  39. return {
  40. root,
  41. artifacts: join(root, 'artifacts'),
  42. runtime: join(root, 'runtime'),
  43. packageSet: join(root, 'package-set'),
  44. dsh: join(root, 'dsh'),
  45. dshPnpm: join(root, 'dsh-pnpm'),
  46. electron: join(root, 'electron'),
  47. packedDsh: join(packed, 'dsh'),
  48. packedVendor: join(packed, 'vendor'),
  49. packedLandlock: join(packed, 'landlock'),
  50. downloads: join(BUILD_ROOT, 'downloads'),
  51. }
  52. }
  53. /**
  54. * Resolve the paths owned by the target selected in a packaging environment.
  55. * @param {NodeJS.ProcessEnv} env - Packaging environment.
  56. * @param {NodeJS.Platform} hostPlatform - Build-host platform used when no target override exists.
  57. * @param {string} hostArch - Build-host architecture used when no target override exists.
  58. * @returns {ReturnType<typeof desktopTargetBuildPaths>} Selected target paths.
  59. */
  60. export function resolveDesktopTargetBuildPaths(
  61. env = process.env,
  62. hostPlatform = process.platform,
  63. hostArch = process.arch,
  64. ) {
  65. return desktopTargetBuildPaths(resolveDesktopBuildTarget(env, hostPlatform, hostArch))
  66. }