main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. const setTray = require('./electron/tray')
  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. if (process.mas) app.setName('electron-egg')
  27. async function initialize () {
  28. app.whenReady().then(() => {
  29. createWindow()
  30. app.on('activate', function () {
  31. if (BrowserWindow.getAllWindows().length === 0) {
  32. createWindow()
  33. }
  34. })
  35. })
  36. app.on('window-all-closed', function () {
  37. if (process.platform !== 'darwin') {
  38. console.log('window-all-closed quit')
  39. app.quit()
  40. }
  41. })
  42. // Open url with the default browser
  43. // app.on('web-contents-created', (e, webContents) => {
  44. // webContents.on('new-window', (event, url) => {
  45. // event.preventDefault()
  46. // shell.openExternal(url)
  47. // });
  48. // });
  49. }
  50. async function createWindow () {
  51. MAIN_WINDOW = new BrowserWindow(electronConfig.get('windowsOption'))
  52. // if (process.platform === 'linux') {
  53. // windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  54. // }
  55. if (eggConfig.env === 'prod') {
  56. // hidden menu
  57. Menu.setApplicationMenu(null)
  58. // dynamic port
  59. await storage.setDynamicPort()
  60. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  61. }
  62. // loding page
  63. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
  64. // tray
  65. setTray();
  66. // egg server
  67. await startServer(eggConfig)
  68. // check update
  69. const updateConfig = electronConfig.get('autoUpdate')
  70. if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
  71. || (is.linux() && updateConfig.linux)) {
  72. const autoUpdater = require('./electron/autoUpdater');
  73. autoUpdater.checkUpdate();
  74. }
  75. return MAIN_WINDOW
  76. }
  77. async function startServer (options) {
  78. ELog.info('[main] [startServer] options', options)
  79. const protocol = 'http://'
  80. let startRes = null
  81. let url = null
  82. if (ENV === 'prod') {
  83. url = protocol + options.hostname + ':' + options.port
  84. } else {
  85. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  86. const selectMode = developmentModeConfig.default
  87. const modeInfo = developmentModeConfig.mode[selectMode]
  88. switch (selectMode) {
  89. case 'vue' :
  90. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  91. break
  92. case 'react' :
  93. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  94. break
  95. case 'ejs' :
  96. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  97. break
  98. }
  99. }
  100. ELog.info('[main] [url]:', url)
  101. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  102. ELog.info('[main] [startServer] startRes:', startRes)
  103. if (startRes === 'success') {
  104. MAIN_WINDOW.loadURL(url)
  105. return true
  106. }
  107. app.relaunch()
  108. }
  109. initialize()