index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. backgroundColor: '#2e2c29'
  67. })
  68. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  69. win.loadURL(url.format({
  70. pathname: path.join(__dirname, 'app', 'app.ejs'),
  71. protocol: 'file:',
  72. slashes: true
  73. }))
  74. /*win.once('ready-to-show', () => {
  75. win.show()
  76. })*/
  77. win.setMenu(null)
  78. win.setResizable(true)
  79. win.on('closed', () => {
  80. win = null
  81. })
  82. }
  83. function getPlatformIcon(filename){
  84. const opSys = process.platform
  85. if (opSys === 'darwin') {
  86. filename = filename + '.icns'
  87. } else if (opSys === 'win32') {
  88. filename = filename + '.ico'
  89. } else {
  90. filename = filename + '.png'
  91. }
  92. return path.join(__dirname, 'app', 'assets', 'images', filename)
  93. }
  94. app.on('ready', createWindow);
  95. app.on('window-all-closed', () => {
  96. // On macOS it is common for applications and their menu bar
  97. // to stay active until the user quits explicitly with Cmd + Q
  98. if (process.platform !== 'darwin') {
  99. app.quit()
  100. }
  101. })
  102. app.on('activate', () => {
  103. // On macOS it's common to re-create a window in the app when the
  104. // dock icon is clicked and there are no other windows open.
  105. if (win === null) {
  106. createWindow()
  107. }
  108. })