main.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const {app, BrowserWindow, Menu, shell} = require('electron')
  2. const path = require('path')
  3. const eggLauncher = require('./electron/lanucher')
  4. const setup = require('./electron/setup')
  5. const electronConfig = require('./electron/config')
  6. const storage = require('./electron/storage')
  7. const autoUpdater = require('./electron/autoUpdater')
  8. // main window
  9. global.MAIN_WINDOW = null
  10. // Initialize
  11. setup()
  12. // return
  13. // argv
  14. let ENV = 'prod'
  15. for (let i = 0; i < process.argv.length; i++) {
  16. const tmpArgv = process.argv[i]
  17. if (tmpArgv.indexOf('--env=') !== -1) {
  18. ENV = tmpArgv.substr(6)
  19. }
  20. }
  21. const eggConfig = electronConfig.get('egg', ENV)
  22. eggConfig.env = ENV
  23. if (process.mas) app.setName('electron-egg')
  24. // Open url with the default browser
  25. app.on('web-contents-created', (e, webContents) => {
  26. webContents.on('new-window', (event, url) => {
  27. event.preventDefault()
  28. shell.openExternal(url)
  29. });
  30. });
  31. async function initialize () {
  32. app.whenReady().then(() => {
  33. createWindow()
  34. app.on('activate', function () {
  35. if (BrowserWindow.getAllWindows().length === 0) {
  36. createWindow()
  37. }
  38. })
  39. })
  40. app.on('window-all-closed', function () {
  41. if (process.platform !== 'darwin') {
  42. console.log('window-all-closed quit')
  43. app.quit()
  44. }
  45. })
  46. }
  47. async function createWindow () {
  48. MAIN_WINDOW = new BrowserWindow(electronConfig.get('windowsOption'))
  49. // if (process.platform === 'linux') {
  50. // windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  51. // }
  52. if (eggConfig.env === 'prod') {
  53. // hidden menu
  54. Menu.setApplicationMenu(null)
  55. // dynamic port
  56. await storage.setDynamicPort()
  57. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  58. }
  59. // loding page
  60. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
  61. // egg server
  62. setTimeout(function(){
  63. startServer(eggConfig)
  64. }, 100)
  65. // check update
  66. const updateConfig = electronConfig.get('autoUpdate')
  67. if (updateConfig.enable) {
  68. autoUpdater.checkUpdate()
  69. }
  70. return MAIN_WINDOW
  71. }
  72. async function startServer (options) {
  73. let startRes = null
  74. ELog.info('[main] [startServer] options', options)
  75. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  76. ELog.info('[main] [startServer] startRes:', startRes)
  77. if (startRes === 'success') {
  78. let url = 'http://localhost:' + options.port
  79. MAIN_WINDOW.loadURL(url)
  80. return true
  81. }
  82. app.relaunch()
  83. }
  84. initialize()