main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. async function initialize () {
  25. app.whenReady().then(() => {
  26. createWindow()
  27. app.on('activate', function () {
  28. if (BrowserWindow.getAllWindows().length === 0) {
  29. createWindow()
  30. }
  31. })
  32. })
  33. app.on('window-all-closed', function () {
  34. if (process.platform !== 'darwin') {
  35. console.log('window-all-closed quit')
  36. app.quit()
  37. }
  38. })
  39. // Open url with the default browser
  40. // app.on('web-contents-created', (e, webContents) => {
  41. // webContents.on('new-window', (event, url) => {
  42. // event.preventDefault()
  43. // shell.openExternal(url)
  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. await startServer(eggConfig)
  63. // check update
  64. const updateConfig = electronConfig.get('autoUpdate')
  65. if (updateConfig.enable) {
  66. autoUpdater.checkUpdate()
  67. }
  68. return MAIN_WINDOW
  69. }
  70. async function startServer (options) {
  71. ELog.info('[main] [startServer] options', options)
  72. const protocol = 'http://'
  73. let startRes = null
  74. let url = null
  75. if (ENV === 'prod') {
  76. url = protocol + options.hostname + ':' + options.port
  77. } else {
  78. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  79. const selectMode = developmentModeConfig.default
  80. const modeInfo = developmentModeConfig.mode[selectMode]
  81. switch (selectMode) {
  82. case 'vue' :
  83. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  84. break
  85. case 'react' :
  86. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  87. break
  88. case 'ejs' :
  89. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  90. break
  91. }
  92. }
  93. ELog.info('[main] [url]:', url)
  94. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  95. ELog.info('[main] [startServer] startRes:', startRes)
  96. if (startRes === 'success') {
  97. MAIN_WINDOW.loadURL(url)
  98. return true
  99. }
  100. app.relaunch()
  101. }
  102. initialize()