index.js 4.9 KB

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