desktop-build-paths.mjs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 ?? hostArch
  20. const os = platform === 'darwin' ? 'mac' : platform === 'win32' || platform === 'win' ? 'win' : platform
  21. const target = `${os}-${arch}`
  22. if (!SUPPORTED_TARGETS.has(target)) {
  23. throw new Error(`desktop build paths: unsupported target ${target}`)
  24. }
  25. return /** @type {'mac-arm64' | 'mac-x64' | 'win-x64'} */ (target)
  26. }
  27. /**
  28. * Return the mutable preparation and artifact directories owned by one release target.
  29. * @param {'mac-arm64' | 'mac-x64' | 'win-x64'} target - Supported Desktop target name.
  30. * @returns {{ root: string, artifacts: string, runtime: string, packageSet: string, seed: string, seedPnpm: string, nodeExtract: string, packedDsh: string, packedVendor: string, packedLandlock: string, downloads: string }} Target paths plus the shared immutable download cache.
  31. */
  32. export function desktopTargetBuildPaths(target) {
  33. if (!SUPPORTED_TARGETS.has(target)) {
  34. throw new Error(`desktop build paths: unsupported target ${String(target)}`)
  35. }
  36. const root = join(BUILD_ROOT, 'targets', target)
  37. const packed = join(root, 'packed')
  38. return {
  39. root,
  40. artifacts: join(root, 'artifacts'),
  41. runtime: join(root, 'runtime'),
  42. packageSet: join(root, 'package-set'),
  43. seed: join(root, 'seed'),
  44. seedPnpm: join(root, 'seed-pnpm'),
  45. nodeExtract: join(root, 'node-extract'),
  46. packedDsh: join(packed, 'dsh'),
  47. packedVendor: join(packed, 'vendor'),
  48. packedLandlock: join(packed, 'landlock'),
  49. downloads: join(BUILD_ROOT, 'downloads'),
  50. }
  51. }
  52. /**
  53. * Resolve the paths owned by the target selected in a packaging environment.
  54. * @param {NodeJS.ProcessEnv} env - Packaging environment.
  55. * @param {NodeJS.Platform} hostPlatform - Build-host platform used when no target override exists.
  56. * @param {string} hostArch - Build-host architecture used when no target override exists.
  57. * @returns {ReturnType<typeof desktopTargetBuildPaths>} Selected target paths.
  58. */
  59. export function resolveDesktopTargetBuildPaths(
  60. env = process.env,
  61. hostPlatform = process.platform,
  62. hostArch = process.arch,
  63. ) {
  64. return desktopTargetBuildPaths(resolveDesktopBuildTarget(env, hostPlatform, hostArch))
  65. }