main.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const {app, BrowserWindow, Menu, shell} = require('electron')
  2. const path = require('path')
  3. const getPort = require('get-port')
  4. const eggLauncher = require('./electron/lanucher')
  5. const setup = require('./electron/index')
  6. const config = require('./electron/config').get()
  7. setup()
  8. // 主窗口
  9. global.MAIN_WINDOW = null
  10. for (let i = 0; i < process.argv.length; i++) {
  11. const tmpArgv = process.argv[i];
  12. if (tmpArgv.indexOf('--env=') !== -1) {
  13. config.egg.env = tmpArgv.substr(6);
  14. }
  15. }
  16. if (process.mas) app.setName('electron-egg')
  17. app.on('web-contents-created', (e, webContents) => {
  18. webContents.on('new-window', (event, url) => {
  19. event.preventDefault();
  20. shell.openExternal(url);
  21. });
  22. });
  23. async function createWindow () {
  24. MAIN_WINDOW = new BrowserWindow(config.windowsOption)
  25. // if (process.platform === 'linux') {
  26. // windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  27. // }
  28. if (config.egg.env === 'prod') {
  29. //隐藏菜单
  30. Menu.setApplicationMenu(null)
  31. }
  32. // loding页
  33. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
  34. // egg服务
  35. setTimeout(function(){
  36. startServer(config.egg)
  37. }, 100)
  38. return MAIN_WINDOW;
  39. }
  40. async function startServer (options) {
  41. let startRes = null;
  42. options.port = await getPort({port: options.port})
  43. ELog.info('config.egg', options);
  44. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  45. ELog.info('startRes:', startRes);
  46. if (startRes === 'success') {
  47. let url = 'http://localhost:' + options.port
  48. MAIN_WINDOW.loadURL(url)
  49. return
  50. }
  51. app.relaunch()
  52. }
  53. async function initialize () {
  54. // loadFiles()
  55. app.whenReady().then(() => {
  56. createWindow()
  57. app.on('activate', function () {
  58. if (BrowserWindow.getAllWindows().length === 0) {
  59. createWindow()
  60. }
  61. })
  62. })
  63. app.on('window-all-closed', function () {
  64. if (process.platform !== 'darwin') {
  65. console.log('window-all-closed quit')
  66. app.quit()
  67. }
  68. })
  69. }
  70. initialize()