Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

install.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env node
  2. const { version } = require('./package');
  3. const childProcess = require('child_process');
  4. const fs = require('fs');
  5. const os = require('os');
  6. const path = require('path');
  7. const extract = require('extract-zip');
  8. const { downloadArtifact } = require('@electron/get');
  9. if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
  10. process.exit(0);
  11. }
  12. const platformPath = getPlatformPath();
  13. if (isInstalled()) {
  14. process.exit(0);
  15. }
  16. const platform = process.env.npm_config_platform || process.platform;
  17. let arch = process.env.npm_config_arch || process.arch;
  18. if (platform === 'darwin' && process.platform === 'darwin' && arch === 'x64' &&
  19. process.env.npm_config_arch === undefined) {
  20. // When downloading for macOS ON macOS and we think we need x64 we should
  21. // check if we're running under rosetta and download the arm64 version if appropriate
  22. try {
  23. const output = childProcess.execSync('sysctl -in sysctl.proc_translated');
  24. if (output.toString().trim() === '1') {
  25. arch = 'arm64';
  26. }
  27. } catch {
  28. // Ignore failure
  29. }
  30. }
  31. // downloads if not cached
  32. downloadArtifact({
  33. version,
  34. artifactName: 'electron',
  35. force: process.env.force_no_cache === 'true',
  36. cacheRoot: process.env.electron_config_cache,
  37. checksums: process.env.electron_use_remote_checksums ?? process.env.npm_config_electron_use_remote_checksums ? undefined : require('./checksums.json'),
  38. platform,
  39. arch
  40. }).then(extractFile).catch(err => {
  41. console.error(err.stack);
  42. process.exit(1);
  43. });
  44. function isInstalled () {
  45. try {
  46. if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
  47. return false;
  48. }
  49. if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
  50. return false;
  51. }
  52. } catch (ignored) {
  53. return false;
  54. }
  55. const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath);
  56. return fs.existsSync(electronPath);
  57. }
  58. // unzips and makes path.txt point at the correct executable
  59. function extractFile (zipPath) {
  60. const distPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist');
  61. return extract(zipPath, { dir: path.join(__dirname, 'dist') }).then(() => {
  62. // If the zip contains an "electron.d.ts" file,
  63. // move that up
  64. const srcTypeDefPath = path.join(distPath, 'electron.d.ts');
  65. const targetTypeDefPath = path.join(__dirname, 'electron.d.ts');
  66. const hasTypeDefinitions = fs.existsSync(srcTypeDefPath);
  67. if (hasTypeDefinitions) {
  68. fs.renameSync(srcTypeDefPath, targetTypeDefPath);
  69. }
  70. // Write a "path.txt" file.
  71. return fs.promises.writeFile(path.join(__dirname, 'path.txt'), platformPath);
  72. });
  73. }
  74. function getPlatformPath () {
  75. const platform = process.env.npm_config_platform || os.platform();
  76. switch (platform) {
  77. case 'mas':
  78. case 'darwin':
  79. return 'Electron.app/Contents/MacOS/Electron';
  80. case 'freebsd':
  81. case 'openbsd':
  82. case 'linux':
  83. return 'electron';
  84. case 'win32':
  85. return 'electron.exe';
  86. default:
  87. throw new Error('Electron builds are not available on platform: ' + platform);
  88. }
  89. }