prepare-runtime.ts 2.7 KB

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