index.js 4.7 KB

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