-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
60 lines (53 loc) · 1.41 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* eslint @typescript-eslint/no-var-requires: "off" */
const http = require('http')
const path = require('path')
const { app, dialog, ipcMain, BrowserWindow } = require('electron')
const isDev = process.env.NODE_ENV === 'dev'
let url = null
if (isDev) {
const { loadNuxt, build } = require('nuxt-edge')
;(async () => {
const nuxt = await loadNuxt('dev')
await build(nuxt)
const server = await nuxt.server.listen()
url = server.url
})()
} else {
url = 'file://' + __dirname + '/.output/public/index.html'
}
// Electron
ipcMain.on('open-dialog', (event, options) => {
event.returnValue = dialog.showOpenDialogSync(options)
})
let win = null
const newWin = () => {
win = new BrowserWindow({
webPreferences: {
preload: path.resolve(path.join(__dirname, 'preload.js')),
nativeWindowOpen: true, // /~https://github.com/electron/electron/issues/28511
},
})
win.setMenu(null)
win.on('closed', () => (win = null))
if (!isDev) {
win.loadURL(url)
return
}
// dev mode
const pollServer = () => {
http
.get(url, (res) => {
if (res.statusCode === 200) {
win && win.loadURL(url)
} else {
setTimeout(pollServer, 300)
}
})
.on('error', pollServer)
}
pollServer()
win.webContents.openDevTools()
}
app.on('ready', newWin)
app.on('window-all-closed', () => app.quit())
app.on('activate', () => win === null && newWin())