main.js 2.6 KB

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