main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const {app, BrowserWindow, Menu} = require('electron')
  2. const path = require('path')
  3. const eggLauncher = require('./electron/lib/lanucher')
  4. const setup = require('./electron/setup')
  5. const electronConfig = require('./electron/config')
  6. const storage = require('./electron/lib/storage')
  7. const preferences = require('./electron/preferences')
  8. // main window
  9. global.MAIN_WINDOW = null
  10. global.APP_TRAY = null;
  11. global.CAN_QUIT = false;
  12. // Initialize
  13. setup()
  14. // argv
  15. let ENV = 'prod'
  16. for (let i = 0; i < process.argv.length; i++) {
  17. const tmpArgv = process.argv[i]
  18. if (tmpArgv.indexOf('--env=') !== -1) {
  19. ENV = tmpArgv.substr(6)
  20. }
  21. }
  22. const eggConfig = electronConfig.get('egg', ENV)
  23. eggConfig.env = ENV
  24. // eLogger
  25. const eLogger = require('./electron/lib/eLogger').get();
  26. if (process.mas) app.setName('electron-egg')
  27. async function initialize () {
  28. app.whenReady().then(() => {
  29. createWindow()
  30. app.on('activate', function () {
  31. if (BrowserWindow.getAllWindows().length === 0) {
  32. createWindow()
  33. }
  34. })
  35. })
  36. app.on('window-all-closed', function () {
  37. if (process.platform !== 'darwin') {
  38. console.log('window-all-closed quit')
  39. app.quit()
  40. }
  41. })
  42. }
  43. async function createWindow () {
  44. MAIN_WINDOW = new BrowserWindow(electronConfig.get('windowsOption'))
  45. if (eggConfig.env === 'prod') {
  46. // hidden menu
  47. Menu.setApplicationMenu(null)
  48. // dynamic port
  49. await storage.setDynamicPort()
  50. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  51. }
  52. // loding page
  53. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/asset/loading.html'))
  54. // options register
  55. preferences()
  56. // egg server
  57. await startServer(eggConfig)
  58. return MAIN_WINDOW
  59. }
  60. async function startServer (options) {
  61. eLogger.info('[main] [startServer] options', options)
  62. const protocol = 'http://'
  63. let startRes = null
  64. let url = null
  65. if (ENV === 'prod') {
  66. url = protocol + options.hostname + ':' + options.port
  67. } else {
  68. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  69. const selectMode = developmentModeConfig.default
  70. const modeInfo = developmentModeConfig.mode[selectMode]
  71. switch (selectMode) {
  72. case 'vue' :
  73. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  74. break
  75. case 'react' :
  76. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  77. break
  78. case 'ejs' :
  79. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  80. break
  81. }
  82. }
  83. eLogger.info('[main] [url]:', url)
  84. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  85. eLogger.info('[main] [startServer] startRes:', startRes)
  86. if (startRes === 'success') {
  87. MAIN_WINDOW.loadURL(url)
  88. return true
  89. }
  90. app.relaunch()
  91. }
  92. initialize()