index.js 3.6 KB

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