main.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if (process.mas) app.setName('electron-egg')
  14. // Open url with the default browser
  15. app.on('web-contents-created', (e, webContents) => {
  16. webContents.on('new-window', (event, url) => {
  17. event.preventDefault()
  18. shell.openExternal(url)
  19. });
  20. });
  21. async function initialize () {
  22. // dynamic port
  23. await storage.setDynamicPort();
  24. app.whenReady().then(() => {
  25. createWindow()
  26. app.on('activate', function () {
  27. if (BrowserWindow.getAllWindows().length === 0) {
  28. createWindow()
  29. }
  30. })
  31. })
  32. app.on('window-all-closed', function () {
  33. if (process.platform !== 'darwin') {
  34. console.log('window-all-closed quit')
  35. app.quit()
  36. }
  37. })
  38. }
  39. async function createWindow () {
  40. // argv
  41. const eggConfig = electronConfig.get('egg')
  42. for (let i = 0; i < process.argv.length; i++) {
  43. const tmpArgv = process.argv[i]
  44. if (tmpArgv.indexOf('--env=') !== -1) {
  45. eggConfig.env = tmpArgv.substr(6)
  46. }
  47. }
  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. }
  56. // loding page
  57. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
  58. // egg server
  59. setTimeout(function(){
  60. startServer(eggConfig)
  61. }, 100)
  62. // check update
  63. const updateConfig = electronConfig.get('autoUpdate')
  64. if (updateConfig.enable) {
  65. autoUpdater.checkUpdate()
  66. }
  67. return MAIN_WINDOW
  68. }
  69. async function startServer (options) {
  70. let startRes = null
  71. ELog.info('[main] [startServer] options', options)
  72. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  73. ELog.info('[main] [startServer] startRes:', startRes)
  74. if (startRes === 'success') {
  75. let url = 'http://localhost:' + options.port
  76. MAIN_WINDOW.loadURL(url)
  77. return true
  78. }
  79. app.relaunch()
  80. }
  81. initialize()