index.js 3.6 KB

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