main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const {app, BrowserWindow, Menu} = require('electron')
  2. const path = require('path')
  3. const eggLauncher = require('./electron/lanucher')
  4. const setup = require('./electron/setup')
  5. const electronConfig = require('./electron/config')
  6. const storage = require('./electron/storage')
  7. const is = require('electron-is')
  8. // main window
  9. global.MAIN_WINDOW = null
  10. // Initialize
  11. setup()
  12. //return
  13. // argv
  14. let ENV = 'prod'
  15. for (let i = 0; i < process.argv.length; i++) {
  16. const tmpArgv = process.argv[i]
  17. if (tmpArgv.indexOf('--env=') !== -1) {
  18. ENV = tmpArgv.substr(6)
  19. }
  20. }
  21. const eggConfig = electronConfig.get('egg', ENV)
  22. eggConfig.env = ENV
  23. if (process.mas) app.setName('electron-egg')
  24. async function initialize () {
  25. app.whenReady().then(() => {
  26. createWindow()
  27. app.on('activate', function () {
  28. if (BrowserWindow.getAllWindows().length === 0) {
  29. createWindow()
  30. }
  31. })
  32. })
  33. app.on('window-all-closed', function () {
  34. if (process.platform !== 'darwin') {
  35. console.log('window-all-closed quit')
  36. app.quit()
  37. }
  38. })
  39. // Open url with the default browser
  40. // app.on('web-contents-created', (e, webContents) => {
  41. // webContents.on('new-window', (event, url) => {
  42. // event.preventDefault()
  43. // shell.openExternal(url)
  44. // });
  45. // });
  46. }
  47. async function createWindow () {
  48. MAIN_WINDOW = new BrowserWindow(electronConfig.get('windowsOption'))
  49. // if (process.platform === 'linux') {
  50. // windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  51. // }
  52. if (eggConfig.env === 'prod') {
  53. // hidden menu
  54. Menu.setApplicationMenu(null)
  55. // dynamic port
  56. await storage.setDynamicPort()
  57. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  58. }
  59. // loding page
  60. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
  61. // egg server
  62. await startServer(eggConfig)
  63. // check update
  64. const updateConfig = electronConfig.get('autoUpdate')
  65. if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
  66. || (is.linux() && updateConfig.linux)) {
  67. const autoUpdater = require('./autoUpdater');
  68. autoUpdater.checkUpdate();
  69. }
  70. return MAIN_WINDOW
  71. }
  72. async function startServer (options) {
  73. ELog.info('[main] [startServer] options', options)
  74. const protocol = 'http://'
  75. let startRes = null
  76. let url = null
  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. ELog.info('[main] [url]:', url)
  96. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  97. ELog.info('[main] [startServer] startRes:', startRes)
  98. if (startRes === 'success') {
  99. MAIN_WINDOW.loadURL(url)
  100. return true
  101. }
  102. app.relaunch()
  103. }
  104. initialize()