main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 is = require('electron-is')
  8. const setTray = require('./electron/lib/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. // 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. // tray
  67. setTray();
  68. // egg server
  69. await startServer(eggConfig)
  70. // check update
  71. const updateConfig = electronConfig.get('autoUpdate')
  72. if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
  73. || (is.linux() && updateConfig.linux)) {
  74. const autoUpdater = require('./electron/autoUpdater');
  75. autoUpdater.checkUpdate();
  76. }
  77. return MAIN_WINDOW
  78. }
  79. async function startServer (options) {
  80. eLogger.info('[main] [startServer] options', options)
  81. const protocol = 'http://'
  82. let startRes = null
  83. let url = null
  84. if (ENV === 'prod') {
  85. url = protocol + options.hostname + ':' + options.port
  86. } else {
  87. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  88. const selectMode = developmentModeConfig.default
  89. const modeInfo = developmentModeConfig.mode[selectMode]
  90. switch (selectMode) {
  91. case 'vue' :
  92. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  93. break
  94. case 'react' :
  95. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  96. break
  97. case 'ejs' :
  98. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  99. break
  100. }
  101. }
  102. eLogger.info('[main] [url]:', url)
  103. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  104. eLogger.info('[main] [startServer] startRes:', startRes)
  105. if (startRes === 'success') {
  106. MAIN_WINDOW.loadURL(url)
  107. return true
  108. }
  109. app.relaunch()
  110. }
  111. initialize()