gaoshuaixing 5 년 전
부모
커밋
f9308e4fc3
2개의 변경된 파일29개의 추가작업 그리고 32개의 파일을 삭제
  1. 19 0
      electron/config.js
  2. 10 32
      main.js

+ 19 - 0
electron/config.js

@@ -12,6 +12,25 @@ exports.get = function () {
         format: '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}',
         maxSize: '1048576' // 1048576 (1mb) by default.
       }
+    },
+    windowsOption: {
+      width: 800,
+      height: 600,
+      minWidth: 800,
+      minHeight: 600,
+      webPreferences: {
+        //webSecurity: false,
+        nodeIntegration: true,
+        preload: path.join(__dirname, 'preload.js')
+      },
+      //frame: false,
+      //titleBarStyle: 'hidden'
+    },
+    egg: {
+      title: 'electron-egg',
+      env: 'prod',
+      port: 7068,
+      workers: 1
     }
   }
 

+ 10 - 32
main.js

@@ -3,24 +3,19 @@ const path = require('path')
 const getPort = require('get-port')
 const eggLauncher = require('./electron/lanucher')
 const setup = require('./electron/index')
+const config = require('./electron/config').get()
 
 setup()
 
 // 主窗口
 global.MAIN_WINDOW = null
 
-let options = {
-  env: 'prod',
-  eggPort: 7068,
-  workers: 1
-};
 for (let i = 0; i < process.argv.length; i++) {
   const tmpArgv = process.argv[i];
   if (tmpArgv.indexOf('--env=') !== -1) {
-    options.env = tmpArgv.substr(6);
+    config.egg.env = tmpArgv.substr(6);
   }
-}  
-ELog.info('options', options);
+}
 
 if (process.mas) app.setName('electron-egg')
 
@@ -32,25 +27,13 @@ app.on('web-contents-created', (e, webContents) => {
 });
 
 async function createWindow () {
-  MAIN_WINDOW = new BrowserWindow({
-    width: 800,
-    height: 600,
-    minWidth: 800,
-    minHeight: 600,
-    webPreferences: {
-      //webSecurity: false,
-      nodeIntegration: true,
-      preload: path.join(__dirname, 'preload.js')
-    },
-    //frame: false,
-    //titleBarStyle: 'hidden'
-  })
+  MAIN_WINDOW = new BrowserWindow(config.windowsOption)
 
   // if (process.platform === 'linux') {
   //   windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
   // }
 
-  if (options.env === 'prod') {
+  if (config.egg.env === 'prod') {
     //隐藏菜单
     Menu.setApplicationMenu(null)
   }
@@ -60,7 +43,7 @@ async function createWindow () {
   
   // egg服务
   setTimeout(function(){
-    startServer(options)
+    startServer(config.egg)
   }, 100)
 
   return MAIN_WINDOW;
@@ -68,17 +51,12 @@ async function createWindow () {
 
 async function startServer (options) {
   let startRes = null;
-  options.eggPort = await getPort({port: options.eggPort})
-  let params = {
-    port: options.eggPort,
-    title: 'electron-egg',
-    workers: 1,
-    env: options.env
-  }
-  startRes = await eggLauncher.start(params).then((res) => res, (err) => err)
+  options.port = await getPort({port: options.port})
+  ELog.info('config.egg', options);
+  startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
   ELog.info('startRes:', startRes);
   if (startRes === 'success') {
-    let url = 'http://localhost:' + options.eggPort
+    let url = 'http://localhost:' + options.port
     MAIN_WINDOW.loadURL(url)
 
     return