index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // Redirect distribution index event from preloader to renderer.
  52. ipcMain.on('distributionIndexDone', (event, arg) => {
  53. event.sender.send('distributionIndexDone', arg)
  54. })
  55. // Disable hardware acceleration.
  56. // https://electronjs.org/docs/tutorial/offscreen-rendering
  57. app.disableHardwareAcceleration()
  58. // Keep a global reference of the window object, if you don't, the window will
  59. // be closed automatically when the JavaScript object is garbage collected.
  60. let win
  61. function createWindow() {
  62. win = new BrowserWindow({
  63. width: 980,
  64. height: 552,
  65. icon: getPlatformIcon('WesterosSealSquare'),
  66. frame: false,
  67. webPreferences: {
  68. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js')
  69. },
  70. backgroundColor: '#2e2c29'
  71. })
  72. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  73. win.loadURL(url.format({
  74. pathname: path.join(__dirname, 'app', 'app.ejs'),
  75. protocol: 'file:',
  76. slashes: true
  77. }))
  78. /*win.once('ready-to-show', () => {
  79. win.show()
  80. })*/
  81. win.setMenu(null)
  82. win.setResizable(true)
  83. win.on('closed', () => {
  84. win = null
  85. })
  86. }
  87. function getPlatformIcon(filename){
  88. const opSys = process.platform
  89. if (opSys === 'darwin') {
  90. filename = filename + '.icns'
  91. } else if (opSys === 'win32') {
  92. filename = filename + '.ico'
  93. } else {
  94. filename = filename + '.png'
  95. }
  96. return path.join(__dirname, 'app', 'assets', 'images', filename)
  97. }
  98. app.on('ready', createWindow);
  99. app.on('window-all-closed', () => {
  100. // On macOS it is common for applications and their menu bar
  101. // to stay active until the user quits explicitly with Cmd + Q
  102. if (process.platform !== 'darwin') {
  103. app.quit()
  104. }
  105. })
  106. app.on('activate', () => {
  107. // On macOS it's common to re-create a window in the app when the
  108. // dock icon is clicked and there are no other windows open.
  109. if (win === null) {
  110. createWindow()
  111. }
  112. })