main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const {app, BrowserWindow, BrowserView, 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. const helper = require('./electron/lib/helper')
  9. // main window
  10. global.MAIN_WINDOW = null;
  11. global.APP_TRAY = null;
  12. global.CAN_QUIT = false;
  13. // Initialize
  14. setup()
  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. // loading html
  28. const loadingHtml = path.join('file://', __dirname, '/asset/loading.html')
  29. async function initialize () {
  30. app.whenReady().then(() => {
  31. createWindow()
  32. app.on('activate', function () {
  33. if (BrowserWindow.getAllWindows().length === 0) {
  34. createWindow()
  35. }
  36. })
  37. })
  38. app.on('window-all-closed', function () {
  39. if (process.platform !== 'darwin') {
  40. eLogger.info('[main] [initialize] window-all-closed quit')
  41. helper.appQuit()
  42. }
  43. })
  44. }
  45. async function createWindow () {
  46. const winOptions = electronConfig.get('windowsOption')
  47. MAIN_WINDOW = new BrowserWindow(winOptions)
  48. // loading html
  49. loadingView(winOptions)
  50. if (eggConfig.env === 'prod') {
  51. // hidden menu
  52. Menu.setApplicationMenu(null)
  53. // dynamic port
  54. await storage.setDynamicPort()
  55. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  56. }
  57. // options register
  58. await preferences()
  59. // egg server
  60. await startServer(eggConfig)
  61. return MAIN_WINDOW
  62. }
  63. async function startServer (options) {
  64. eLogger.info('[main] [startServer] options', options)
  65. const protocol = 'http://'
  66. let startRes = null
  67. let url = null
  68. const remoteConfig = electronConfig.get('remoteUrl');
  69. if (remoteConfig.enable) {
  70. url = remoteConfig.url
  71. loadMainUrl(url)
  72. return true
  73. }
  74. if (ENV === 'prod') {
  75. url = protocol + options.hostname + ':' + options.port
  76. } else {
  77. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  78. const selectMode = developmentModeConfig.default
  79. const modeInfo = developmentModeConfig.mode[selectMode]
  80. switch (selectMode) {
  81. case 'vue' :
  82. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  83. break
  84. case 'react' :
  85. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  86. break
  87. case 'ejs' :
  88. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  89. break
  90. }
  91. }
  92. eLogger.info('[main] [url]:', url)
  93. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  94. eLogger.info('[main] [startServer] startRes:', startRes)
  95. if (startRes === 'success') {
  96. loadMainUrl(url)
  97. return true
  98. }
  99. app.relaunch()
  100. }
  101. /**
  102. * White screen optimization
  103. */
  104. function loadingView (winOptions) {
  105. const loadingBrowserView = new BrowserView()
  106. MAIN_WINDOW.setBrowserView(loadingBrowserView)
  107. loadingBrowserView.setBounds({
  108. x: 0,
  109. y: 0,
  110. width: winOptions.width,
  111. height: winOptions.height
  112. });
  113. loadingBrowserView.webContents.loadURL(loadingHtml)
  114. MAIN_WINDOW.webContents.on('dom-ready', async (event) => {
  115. MAIN_WINDOW.removeBrowserView(loadingBrowserView)
  116. });
  117. }
  118. function loadMainUrl (url) {
  119. MAIN_WINDOW.loadURL(url)
  120. }
  121. initialize()