main.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const {app, BrowserWindow, Menu, shell} = require('electron')
  2. const path = require('path')
  3. const getPort = require('get-port')
  4. const eggLauncher = require('./app/lanucher')
  5. // glogger
  6. global.GLOGGER = require('electron-log')
  7. GLOGGER.transports.console.level = 'silly'
  8. GLOGGER.transports.file.file = './logs/main.log'
  9. // 主窗口
  10. global.MAIN_WINDOW = null
  11. // console.log('path:', app.getAppPath())
  12. // return;
  13. let options = {
  14. env: 'prod',
  15. eggPort: 7068,
  16. workers: 1
  17. };
  18. for (let i = 0; i < process.argv.length; i++) {
  19. const tmpArgv = process.argv[i];
  20. if (tmpArgv.indexOf('--env=') !== -1) {
  21. options.env = tmpArgv.substr(6);
  22. }
  23. }
  24. GLOGGER.info('options', options);
  25. if (process.mas) app.setName('electron-egg')
  26. app.on('web-contents-created', (e, webContents) => {
  27. webContents.on('new-window', (event, url) => {
  28. event.preventDefault();
  29. shell.openExternal(url);
  30. });
  31. });
  32. async function createWindow () {
  33. MAIN_WINDOW = new BrowserWindow({
  34. width: 800,
  35. height: 600,
  36. minWidth: 800,
  37. minHeight: 600,
  38. webPreferences: {
  39. //webSecurity: false,
  40. nodeIntegration: true,
  41. preload: path.join(__dirname, 'preload.js')
  42. },
  43. //frame: false,
  44. //titleBarStyle: 'hidden'
  45. })
  46. // if (process.platform === 'linux') {
  47. // windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  48. // }
  49. if (options.env === 'prod') {
  50. //隐藏菜单
  51. Menu.setApplicationMenu(null)
  52. }
  53. // loding页
  54. MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
  55. // egg服务
  56. setTimeout(function(){
  57. startServer(options)
  58. }, 100)
  59. return MAIN_WINDOW;
  60. }
  61. async function startServer (options) {
  62. let startRes = null;
  63. options.eggPort = await getPort({port: options.eggPort})
  64. let params = {
  65. port: options.eggPort,
  66. title: 'electron-egg',
  67. workers: 1,
  68. env: options.env
  69. }
  70. startRes = await eggLauncher.start(params).then((res) => res, (err) => err)
  71. GLOGGER.info('startRes:', startRes);
  72. if (startRes === 'success') {
  73. let url = 'http://localhost:' + options.eggPort
  74. MAIN_WINDOW.loadURL(url)
  75. return
  76. }
  77. app.relaunch()
  78. }
  79. async function initialize () {
  80. app.whenReady().then(() => {
  81. createWindow()
  82. app.on('activate', function () {
  83. if (BrowserWindow.getAllWindows().length === 0) {
  84. createWindow()
  85. }
  86. })
  87. })
  88. app.on('window-all-closed', function () {
  89. if (process.platform !== 'darwin') {
  90. console.log('window-all-closed quit')
  91. app.quit()
  92. }
  93. })
  94. }
  95. initialize()