install.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // When downloading for macOS ON macOS and we think we need x64 we should
  20. // check if we're running under rosetta and download the arm64 version if appropriate
  21. try {
  22. const output = childProcess.execSync('sysctl -in sysctl.proc_translated');
  23. if (output.toString().trim() === '1') {
  24. arch = 'arm64';
  25. }
  26. } catch {
  27. // Ignore failure
  28. }
  29. }
  30. // downloads if not cached
  31. downloadArtifact({
  32. version,
  33. artifactName: 'electron',
  34. force: process.env.force_no_cache === 'true',
  35. cacheRoot: process.env.electron_config_cache,
  36. checksums: process.env.electron_use_remote_checksums ? undefined : require('./checksums.json'),
  37. platform,
  38. arch
  39. }).then(extractFile).catch(err => {
  40. console.error(err.stack);
  41. process.exit(1);
  42. });
  43. function isInstalled () {
  44. try {
  45. if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
  46. return false;
  47. }
  48. if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
  49. return false;
  50. }
  51. } catch (ignored) {
  52. return false;
  53. }
  54. const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath);
  55. return fs.existsSync(electronPath);
  56. }
  57. // unzips and makes path.txt point at the correct executable
  58. function extractFile (zipPath) {
  59. return new Promise((resolve, reject) => {
  60. extract(zipPath, { dir: path.join(__dirname, 'dist') }, err => {
  61. if (err) return reject(err);
  62. fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, err => {
  63. if (err) return reject(err);
  64. resolve();
  65. });
  66. });
  67. });
  68. }
  69. function getPlatformPath () {
  70. const platform = process.env.npm_config_platform || os.platform();
  71. switch (platform) {
  72. case 'mas':
  73. case 'darwin':
  74. return 'Electron.app/Contents/MacOS/Electron';
  75. case 'freebsd':
  76. case 'openbsd':
  77. case 'linux':
  78. return 'electron';
  79. case 'win32':
  80. return 'electron.exe';
  81. default:
  82. throw new Error('Electron builds are not available on platform: ' + platform);
  83. }
  84. }