install.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env node
  2. var version = require('./package').version
  3. var fs = require('fs')
  4. var os = require('os')
  5. var path = require('path')
  6. var extract = require('extract-zip')
  7. var download = require('electron-download')
  8. var installedVersion = null
  9. try {
  10. installedVersion = fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '')
  11. } catch (ignored) {
  12. // do nothing
  13. }
  14. var platformPath = getPlatformPath()
  15. if (installedVersion === version && fs.existsSync(path.join(__dirname, platformPath))) {
  16. process.exit(0)
  17. }
  18. // downloads if not cached
  19. download({
  20. cache: process.env.electron_config_cache,
  21. version: version,
  22. platform: process.env.npm_config_platform,
  23. arch: process.env.npm_config_arch,
  24. strictSSL: process.env.npm_config_strict_ssl === 'true',
  25. force: process.env.force_no_cache === 'true',
  26. quiet: process.env.npm_config_loglevel === 'silent' || process.env.CI
  27. }, extractFile)
  28. // unzips and makes path.txt point at the correct executable
  29. function extractFile (err, zipPath) {
  30. if (err) return onerror(err)
  31. extract(zipPath, {dir: path.join(__dirname, 'dist')}, function (err) {
  32. if (err) return onerror(err)
  33. fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, function (err) {
  34. if (err) return onerror(err)
  35. })
  36. })
  37. }
  38. function onerror (err) {
  39. throw err
  40. }
  41. function getPlatformPath () {
  42. var platform = process.env.npm_config_platform || os.platform()
  43. switch (platform) {
  44. case 'darwin':
  45. return 'Electron.app/Contents/MacOS/Electron'
  46. case 'freebsd':
  47. case 'linux':
  48. return 'electron'
  49. case 'win32':
  50. return 'electron.exe'
  51. default:
  52. throw new Error('Electron builds are not available on platform: ' + platform)
  53. }
  54. }