index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Requirements
  2. const {app, BrowserWindow, ipcMain} = require('electron')
  3. const autoUpdater = require('electron-updater').autoUpdater
  4. const ConfigManager = require('./app/assets/js/configmanager.js')
  5. const ejse = require('ejs-electron')
  6. const fs = require('fs')
  7. const isDev = require('electron-is-dev')
  8. const path = require('path')
  9. const semver = require('semver')
  10. const url = require('url')
  11. // Setup auto updater.
  12. function initAutoUpdater(event) {
  13. if(ConfigManager.getAllowPrerelease()){
  14. autoUpdater.allowPrerelease = true
  15. } else {
  16. // Defaults to true if application version contains prerelease components (e.g. 0.12.1-alpha.1)
  17. // autoUpdater.allowPrerelease = true
  18. }
  19. if(isDev){
  20. autoUpdater.autoInstallOnAppQuit = false
  21. autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml')
  22. }
  23. autoUpdater.on('update-available', (info) => {
  24. event.sender.send('autoUpdateNotification', 'update-available', info)
  25. })
  26. autoUpdater.on('update-downloaded', (info) => {
  27. event.sender.send('autoUpdateNotification', 'update-downloaded', info)
  28. })
  29. autoUpdater.on('update-not-available', (info) => {
  30. event.sender.send('autoUpdateNotification', 'update-not-available', info)
  31. })
  32. autoUpdater.on('checking-for-update', () => {
  33. event.sender.send('autoUpdateNotification', 'checking-for-update')
  34. })
  35. }
  36. // Open channel to listen for update actions.
  37. ipcMain.on('autoUpdateAction', (event, arg, data) => {
  38. switch(arg){
  39. case 'initAutoUpdater':
  40. console.log('Initializing auto updater.')
  41. initAutoUpdater(event)
  42. event.sender.send('autoUpdateNotification', 'ready')
  43. break
  44. case 'checkForUpdate':
  45. autoUpdater.checkForUpdates()
  46. .catch(err => {
  47. event.sender.send('autoUpdateNotification', 'realerror', err)
  48. })
  49. break
  50. case 'allowPrereleaseChange':
  51. if(!data){
  52. const preRelComp = semver.prerelease(app.getVersion())
  53. if(preRelComp != null && preRelComp.length > 0){
  54. autoUpdater.allowPrerelease = true
  55. } else {
  56. autoUpdater.allowPrerelease = data
  57. }
  58. } else {
  59. autoUpdater.allowPrerelease = data
  60. }
  61. break
  62. case 'installUpdateNow':
  63. autoUpdater.quitAndInstall()
  64. break
  65. default:
  66. console.log('Unknown argument', arg)
  67. break
  68. }
  69. })
  70. // Redirect distribution index event from preloader to renderer.
  71. ipcMain.on('distributionIndexDone', (event, data) => {
  72. event.sender.send('distributionIndexDone', data)
  73. })
  74. // Disable hardware acceleration.
  75. // https://electronjs.org/docs/tutorial/offscreen-rendering
  76. app.disableHardwareAcceleration()
  77. // Keep a global reference of the window object, if you don't, the window will
  78. // be closed automatically when the JavaScript object is garbage collected.
  79. let win
  80. function createWindow() {
  81. win = new BrowserWindow({
  82. width: 980,
  83. height: 552,
  84. icon: getPlatformIcon('WesterosSealSquare'),
  85. frame: false,
  86. webPreferences: {
  87. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js')
  88. },
  89. backgroundColor: '#171614'
  90. })
  91. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  92. win.loadURL(url.format({
  93. pathname: path.join(__dirname, 'app', 'app.ejs'),
  94. protocol: 'file:',
  95. slashes: true
  96. }))
  97. /*win.once('ready-to-show', () => {
  98. win.show()
  99. })*/
  100. win.setMenu(null)
  101. win.setResizable(true)
  102. win.on('closed', () => {
  103. win = null
  104. })
  105. }
  106. function getPlatformIcon(filename){
  107. const opSys = process.platform
  108. if (opSys === 'darwin') {
  109. filename = filename + '.icns'
  110. } else if (opSys === 'win32') {
  111. filename = filename + '.ico'
  112. } else {
  113. filename = filename + '.png'
  114. }
  115. return path.join(__dirname, 'app', 'assets', 'images', filename)
  116. }
  117. app.on('ready', createWindow);
  118. app.on('window-all-closed', () => {
  119. // On macOS it is common for applications and their menu bar
  120. // to stay active until the user quits explicitly with Cmd + Q
  121. if (process.platform !== 'darwin') {
  122. app.quit()
  123. }
  124. })
  125. app.on('activate', () => {
  126. // On macOS it's common to re-create a window in the app when the
  127. // dock icon is clicked and there are no other windows open.
  128. if (win === null) {
  129. createWindow()
  130. }
  131. })