prepare-runtime.ts 2.5 KB

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