main.js 3.6 KB

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