main.js 2.2 KB

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