-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (47 loc) · 1.53 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
const { app, Menu, Tray, Notification } = require('electron')
const { scheduleSystemCheck, scheduler } = require('./src/systemcheck')
const { getDoNotDisturb } = require('electron-notification-state')
const { api } = require('./src/server.js')
const path = require('path')
let tray = null
const port = 1761
const version = '0.0.6'
const contextMenu = [
{ label: `API ${version}: running..` },
{ label: 'Close', click() { scheduler.stop(); app.quit() } },
]
function startUpNotification() {
if (getDoNotDisturb()) return
new Notification({
title: 'Printer API started!',
body: 'You can now list printers.',
silent: true
}).show()
}
const gotTheLock = app.requestSingleInstanceLock()
if (! gotTheLock) app.quit()
function startRestApi() {
api.listen(port, '127.0.0.1', () => {
console.log(`REST service started on http://127.0.0.1:${port}.`)
})
api.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
new Notification({
title: 'Printer API service already running.',
silent: true
}).show()
}
})
}
if (process.platform === 'win32') app.setAppUserModelId(process.execPath);
function startApp() {
const icon = path.join(__dirname, 'src/[email protected]')
tray = new Tray(icon)
tray.setToolTip('Printer Client')
tray.setContextMenu(Menu.buildFromTemplate(contextMenu))
scheduleSystemCheck(port, tray, contextMenu)
startRestApi()
}
app.whenReady()
.then(startApp)
.then(startUpNotification);