install.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env node
  2. const version = require('./package').version
  3. const fs = require('fs')
  4. const os = require('os')
  5. const path = require('path')
  6. const extract = require('extract-zip')
  7. const { downloadArtifact } = require('@electron/get')
  8. if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) {
  9. process.exit(0)
  10. }
  11. const platformPath = getPlatformPath()
  12. if (isInstalled()) {
  13. process.exit(0)
  14. }
  15. // downloads if not cached
  16. downloadArtifact({
  17. version,
  18. artifactName: 'electron',
  19. force: process.env.force_no_cache === 'true',
  20. cacheRoot: process.env.electron_config_cache,
  21. platform: process.env.npm_config_platform || process.platform,
  22. arch: process.env.npm_config_arch || process.arch
  23. }).then(extractFile).catch(err => {
  24. console.error(err.stack)
  25. process.exit(1)
  26. })
  27. function isInstalled () {
  28. try {
  29. if (fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') !== version) {
  30. return false
  31. }
  32. if (fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8') !== platformPath) {
  33. return false
  34. }
  35. } catch (ignored) {
  36. return false
  37. }
  38. const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(__dirname, 'dist', platformPath)
  39. return fs.existsSync(electronPath)
  40. }
  41. // unzips and makes path.txt point at the correct executable
  42. function extractFile (zipPath) {
  43. return new Promise((resolve, reject) => {
  44. extract(zipPath, { dir: path.join(__dirname, 'dist') }, err => {
  45. if (err) return reject(err)
  46. fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, err => {
  47. if (err) return reject(err)
  48. resolve()
  49. })
  50. })
  51. })
  52. }
  53. function getPlatformPath () {
  54. const platform = process.env.npm_config_platform || os.platform()
  55. switch (platform) {
  56. case 'mas':
  57. case 'darwin':
  58. return 'Electron.app/Contents/MacOS/Electron'
  59. case 'freebsd':
  60. case 'openbsd':
  61. case 'linux':
  62. return 'electron'
  63. case 'win32':
  64. return 'electron.exe'
  65. default:
  66. throw new Error('Electron builds are not available on platform: ' + platform)
  67. }
  68. }