main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const {app, BrowserWindow, 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 setTray = require('./electron/lib/tray')
  8. const preferences = require('./electron/preferences')
  9. // main window
  10. global.MAIN_WINDOW = null
  11. global.APP_TRAY = null;
  12. global.CAN_QUIT = false;
  13. // Initialize
  14. setup()
  15. //return
  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. if (process.mas) app.setName('electron-egg')
  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. console.log('window-all-closed quit')
  41. app.quit()
  42. }
  43. })
  44. // Open url with the default browser
  45. // app.on('web-contents-created', (e, webContents) => {
  46. // webContents.on('new-window', (event, url) => {
  47. // event.preventDefault()
  48. // shell.openExternal(url)
  49. // });
  50. // });
  51. }
  52. async function createWindow () {
  53. MAIN_WINDOW = new BrowserWindow(electronConfig.get('windowsOption'))
  54. // if (process.platform === 'linux') {
  55. // windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  56. // }
  57. if (eggConfig.env === 'prod') {
  58. // hidden menu
  59. Menu.setApplicationMenu(null)
  60. // dynamic port
  61. await storage.setDynamicPort()
  62. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  63. }
  64. // loding page
  65. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/asset/loading.html'))
  66. // options register
  67. preferences()
  68. // egg server
  69. await startServer(eggConfig)
  70. return MAIN_WINDOW
  71. }
  72. async function startServer (options) {
  73. eLogger.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. 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. MAIN_WINDOW.loadURL(url)
  100. return true
  101. }
  102. app.relaunch()
  103. }
  104. initialize()