|
|
@@ -1,25 +1,20 @@
|
|
|
const {app, BrowserWindow, Menu, shell} = require('electron')
|
|
|
const path = require('path')
|
|
|
-const getPort = require('get-port')
|
|
|
const eggLauncher = require('./electron/lanucher')
|
|
|
const setup = require('./electron/setup')
|
|
|
-const config = require('./electron/config').get()
|
|
|
+const electronConfig = require('./electron/config')
|
|
|
+const storage = require('./electron/storage')
|
|
|
|
|
|
+// Initialize
|
|
|
setup()
|
|
|
+// return
|
|
|
|
|
|
-//return
|
|
|
-// 主窗口
|
|
|
+// main window
|
|
|
global.MAIN_WINDOW = null
|
|
|
|
|
|
-for (let i = 0; i < process.argv.length; i++) {
|
|
|
- const tmpArgv = process.argv[i]
|
|
|
- if (tmpArgv.indexOf('--env=') !== -1) {
|
|
|
- config.egg.env = tmpArgv.substr(6)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
if (process.mas) app.setName('electron-egg')
|
|
|
|
|
|
+// Open url with the default browser
|
|
|
app.on('web-contents-created', (e, webContents) => {
|
|
|
webContents.on('new-window', (event, url) => {
|
|
|
event.preventDefault()
|
|
|
@@ -27,24 +22,55 @@ app.on('web-contents-created', (e, webContents) => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+async function initialize () {
|
|
|
+
|
|
|
+ // dynamic port
|
|
|
+ await storage.setDynamicPort();
|
|
|
+
|
|
|
+ app.whenReady().then(() => {
|
|
|
+ createWindow()
|
|
|
+ app.on('activate', function () {
|
|
|
+ if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
+ createWindow()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ app.on('window-all-closed', function () {
|
|
|
+ if (process.platform !== 'darwin') {
|
|
|
+ console.log('window-all-closed quit')
|
|
|
+ app.quit()
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
async function createWindow () {
|
|
|
- MAIN_WINDOW = new BrowserWindow(config.windowsOption)
|
|
|
+ // argv
|
|
|
+ const eggConfig = electronConfig.get('egg')
|
|
|
+ for (let i = 0; i < process.argv.length; i++) {
|
|
|
+ const tmpArgv = process.argv[i]
|
|
|
+ if (tmpArgv.indexOf('--env=') !== -1) {
|
|
|
+ eggConfig.env = tmpArgv.substr(6)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ MAIN_WINDOW = new BrowserWindow(electronConfig.get('windowsOption'))
|
|
|
+
|
|
|
// if (process.platform === 'linux') {
|
|
|
// windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
|
|
|
// }
|
|
|
|
|
|
- if (config.egg.env === 'prod') {
|
|
|
- //隐藏菜单
|
|
|
+ if (eggConfig.env === 'prod') {
|
|
|
+ // hidden menu
|
|
|
Menu.setApplicationMenu(null)
|
|
|
}
|
|
|
|
|
|
- // loding页
|
|
|
+ // loding page
|
|
|
MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/app/public/loading.html'))
|
|
|
|
|
|
- // egg服务
|
|
|
+ // egg server
|
|
|
setTimeout(function(){
|
|
|
- startServer(config.egg)
|
|
|
+ startServer(eggConfig)
|
|
|
}, 100)
|
|
|
|
|
|
return MAIN_WINDOW
|
|
|
@@ -52,35 +78,16 @@ async function createWindow () {
|
|
|
|
|
|
async function startServer (options) {
|
|
|
let startRes = null
|
|
|
- options.port = await getPort({port: options.port})
|
|
|
- ELog.info('config.egg', options)
|
|
|
+ ELog.info('[main] [startServer] options', options)
|
|
|
startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
|
|
|
ELog.info('startRes:', startRes)
|
|
|
if (startRes === 'success') {
|
|
|
let url = 'http://localhost:' + options.port
|
|
|
MAIN_WINDOW.loadURL(url)
|
|
|
|
|
|
- return
|
|
|
+ return true
|
|
|
}
|
|
|
app.relaunch()
|
|
|
-}
|
|
|
-
|
|
|
-async function initialize () {
|
|
|
- app.whenReady().then(() => {
|
|
|
- createWindow()
|
|
|
- app.on('activate', function () {
|
|
|
- if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
- createWindow()
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
- app.on('window-all-closed', function () {
|
|
|
- if (process.platform !== 'darwin') {
|
|
|
- console.log('window-all-closed quit')
|
|
|
- app.quit()
|
|
|
- }
|
|
|
- })
|
|
|
}
|
|
|
|
|
|
initialize()
|