-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.ts
117 lines (90 loc) · 2.83 KB
/
app.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Debug = require('debug')
import { app, EventEmitter, ipcMain } from 'electron'
import { resolve } from 'path'
import { install as installExifRotate } from 'electron-exif-rotate'
import { WindowManager } from './window-manager'
const debug = Debug('app:app')
debug(`starting`, app.getName(), 'version', app.getVersion())
export const wm = new WindowManager()
// associate `monux://` urls with app
if (!app.isDefaultProtocolClient(app.getName().toLowerCase())) {
app.setAsDefaultProtocolClient(app.getName().toLowerCase())
}
// load auto reloader in development
if (!app.isPackaged) {
debug('starting auto reloader')
import('electron-reload')
.then(reloader => reloader(resolve(__dirname)))
.catch(console.error)
}
// ensure app is singleton
const instanceLock = app.requestSingleInstanceLock()
if (!instanceLock) {
app.quit()
}
// electron events
app.on('ready', () => {
debug('ready event')
// configure HTTPS interceptor for auto image rotation
installExifRotate()
// load devtool extensions in development
if (!app.isPackaged) loadDevtoolExtensions()
wm.goToMonux()
})
app.on('open-url', async (_, forwardedUrl) => {
debug('open-url event')
forwardAuthUrl(forwardedUrl)
})
app.on('window-all-closed', () => {
debug('window-all-closed event')
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
debug('activate event')
if (wm.hasMainWindow()) wm.focusMainWindow()
else wm.goToMonux()
})
app.on('second-instance', (_ev, argv, _) => {
// focus main window if second instance started
if (wm.hasMainWindow() && wm.mainWindow.isMinimized()) {
wm.mainWindow.restore()
}
wm.focusMainWindow()
if (process.platform === 'win32' && argv.length > 1) {
const authUrl = argv.find(param => {
return param.toLowerCase().startsWith('monux://')
})
if (authUrl) {
forwardAuthUrl(authUrl)
} else {
console.error('Auth URL not found')
throw new Error('Auth URL not found')
}
}
})
// ipc events
ipcMain.on('open-auth-window', (_ev: EventEmitter, url: string) => {
debug('opening auth request window')
wm.openAuthRequest(url)
})
ipcMain.on('auth-success:monzo', () => {
wm.closeAuthRequest()
})
// helpers
function forwardAuthUrl(forwardedUrl: string) {
wm.mainWindow.webContents.send('auth-verify:monzo', forwardedUrl)
}
function loadDevtoolExtensions() {
debug('loading devtool extensions')
import('devtron')
.then(({ install }) => install())
.catch(console.error)
import('electron-devtools-installer')
.then(({ default: installExtension, REDUX_DEVTOOLS }) => {
const extensions = [installExtension(REDUX_DEVTOOLS)]
return Promise.all(extensions)
.then(names => debug('Added Extensions:', names.join(', ')))
.catch(err => debug('An error occurred adding extension:', err))
})
.catch(console.error)
}