prepare-runtime.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /** Prepare the target Electron distribution and pinned pnpm CLI. */
  2. import { execFileSync } from 'node:child_process'
  3. import { chmodSync, cpSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  4. import { createRequire } from 'node:module'
  5. import { dirname, join } from 'node:path'
  6. import { downloadArtifact } from '@electron/get'
  7. import extractZip from 'extract-zip'
  8. import { resolveDesktopBuildTarget, resolveDesktopTargetBuildPaths } from './desktop-build-paths.mjs'
  9. const BUILD_PATHS = resolveDesktopTargetBuildPaths()
  10. const RUNTIME_ROOT = BUILD_PATHS.runtime
  11. function preparePnpm(): string {
  12. const require = createRequire(import.meta.url)
  13. const manifestPath = require.resolve('pnpm')
  14. const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) as { version?: unknown }
  15. if (typeof manifest.version !== 'string') throw new Error('desktop runtime: pnpm manifest has no version')
  16. const packageDir = dirname(manifestPath)
  17. const destination = join(RUNTIME_ROOT, 'pnpm')
  18. rmSync(destination, { recursive: true, force: true })
  19. cpSync(packageDir, destination, { recursive: true })
  20. return manifest.version
  21. }
  22. async function main(): Promise<void> {
  23. const target = resolveDesktopBuildTarget()
  24. const platform = target.startsWith('mac-') ? 'darwin' : 'win32'
  25. const arch = target.endsWith('arm64') ? 'arm64' : 'x64'
  26. const require = createRequire(import.meta.url)
  27. const { version } = require('electron/package.json') as { version: string }
  28. const archive = await downloadArtifact({ version, platform, arch, artifactName: 'electron', cacheRoot: BUILD_PATHS.downloads })
  29. rmSync(BUILD_PATHS.electron, { recursive: true, force: true })
  30. await extractZip(archive, { dir: BUILD_PATHS.electron })
  31. const executable = join(BUILD_PATHS.electron, platform === 'win32' ? 'electron.exe' : 'Electron.app/Contents/MacOS/Electron')
  32. const nodeVersion = execFileSync(executable, ['-p', 'process.versions.node'], {
  33. encoding: 'utf8', env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
  34. }).trim()
  35. rmSync(RUNTIME_ROOT, { recursive: true, force: true })
  36. mkdirSync(RUNTIME_ROOT, { recursive: true })
  37. const pnpmVersion = preparePnpm()
  38. cpSync(join(import.meta.dirname, 'node-bin'), join(RUNTIME_ROOT, 'bin'), { recursive: true })
  39. chmodSync(join(RUNTIME_ROOT, 'bin', 'node'), 0o755)
  40. writeFileSync(join(RUNTIME_ROOT, 'versions.json'), `${JSON.stringify({
  41. schemaVersion: 1,
  42. node: nodeVersion,
  43. pnpm: pnpmVersion,
  44. }, undefined, 2)}\n`)
  45. }
  46. await main()