main.js 3.5 KB

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