main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const {app, BrowserWindow, Menu, shell} = 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 autoUpdater = require('./electron/autoUpdater')
  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 (updateConfig.enable) {
  66. // windows可以开启;macOs 需要签名验证
  67. //autoUpdater.checkUpdate()
  68. }
  69. return MAIN_WINDOW
  70. }
  71. async function startServer (options) {
  72. ELog.info('[main] [startServer] options', options)
  73. const protocol = 'http://'
  74. let startRes = null
  75. let url = null
  76. if (ENV === 'prod') {
  77. url = protocol + options.hostname + ':' + options.port
  78. } else {
  79. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  80. const selectMode = developmentModeConfig.default
  81. const modeInfo = developmentModeConfig.mode[selectMode]
  82. switch (selectMode) {
  83. case 'vue' :
  84. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  85. break
  86. case 'react' :
  87. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  88. break
  89. case 'ejs' :
  90. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  91. break
  92. }
  93. }
  94. ELog.info('[main] [url]:', url)
  95. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  96. ELog.info('[main] [startServer] startRes:', startRes)
  97. if (startRes === 'success') {
  98. MAIN_WINDOW.loadURL(url)
  99. return true
  100. }
  101. app.relaunch()
  102. }
  103. initialize()