index.js 4.8 KB

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