main.js 2.8 KB

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