install.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env node
  2. // maintainer note - x.y.z-ab version in package.json -> x.y.z
  3. var version = require('./package').version.replace(/-.*/, '')
  4. var fs = require('fs')
  5. var os = require('os')
  6. var path = require('path')
  7. var extract = require('extract-zip')
  8. var download = require('electron-download')
  9. var installedVersion = null
  10. try {
  11. installedVersion = fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '')
  12. } catch (ignored) {
  13. // do nothing
  14. }
  15. var platformPath = getPlatformPath()
  16. if (installedVersion === version && fs.existsSync(path.join(__dirname, platformPath))) {
  17. process.exit(0)
  18. }
  19. // downloads if not cached
  20. download({
  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. quiet: ['info', 'verbose', 'silly', 'http'].indexOf(process.env.npm_config_loglevel) === -1
  26. }, extractFile)
  27. // unzips and makes path.txt point at the correct executable
  28. function extractFile (err, zipPath) {
  29. if (err) return onerror(err)
  30. extract(zipPath, {dir: path.join(__dirname, 'dist')}, function (err) {
  31. if (err) return onerror(err)
  32. fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, function (err) {
  33. if (err) return onerror(err)
  34. })
  35. })
  36. }
  37. function onerror (err) {
  38. throw err
  39. }
  40. function getPlatformPath () {
  41. var platform = process.env.npm_config_platform || os.platform()
  42. switch (platform) {
  43. case 'darwin':
  44. return 'dist/Electron.app/Contents/MacOS/Electron'
  45. case 'freebsd':
  46. case 'linux':
  47. return 'dist/electron'
  48. case 'win32':
  49. return 'dist/electron.exe'
  50. default:
  51. throw new Error('Electron builds are not available on platform: ' + platform)
  52. }
  53. }