forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
275 lines (235 loc) · 10.1 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const {
app,
BrowserWindow,
Menu,
MenuItem,
shell,
ipcMain,
} = require('electron');
const _ = require('underscore');
const serve = require('electron-serve');
const contextMenu = require('electron-context-menu');
const {autoUpdater} = require('electron-updater');
const log = require('electron-log');
const ELECTRON_EVENTS = require('./ELECTRON_EVENTS');
const checkForUpdates = require('../src/libs/checkForUpdates');
const isDev = process.env.NODE_ENV === 'development';
const port = process.env.PORT || 8080;
/**
* Electron main process that handles wrapping the web application.
*
* @see: https://www.electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes
*/
// TODO: Turn this off, use web-security after alpha launch, currently we receive a CORS issue preventing
// the electron app from making any API requests.
app.commandLine.appendSwitch('disable-web-security');
// This is necessary for NetInfo to work correctly as it does not handle the NetworkInformation API events correctly
// See: https://github.com/electron/electron/issues/22597
app.commandLine.appendSwitch('enable-network-information-downlink-max');
// Initialize the right click menu
// See https://github.com/sindresorhus/electron-context-menu
contextMenu();
// Send all autoUpdater logs to a log file: ~/Library/Logs/new.expensify/main.log
// See https://www.npmjs.com/package/electron-log
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
// Send all Console logs to a log file: ~/Library/Logs/new.expensify/main.log
// See https://www.npmjs.com/package/electron-log
_.assign(console, log.functions);
// setup Hot reload
if (isDev) {
try {
require('electron-reloader')(module, {
watchRenderer: false,
ignore: [/^(desktop)/],
});
// eslint-disable-next-line no-empty
} catch {}
}
// This sets up the command line arguments used to manage the update. When
// the --expected-update-version flag is set, the app will open pre-hidden
// until it detects that it has been upgraded to the correct version.
const EXPECTED_UPDATE_VERSION_FLAG = '--expected-update-version';
let expectedUpdateVersion;
for (let i = 0; i < process.argv.length; i++) {
const arg = process.argv[i];
if (arg.startsWith(`${EXPECTED_UPDATE_VERSION_FLAG}=`)) {
expectedUpdateVersion = arg.substr((`${EXPECTED_UPDATE_VERSION_FLAG}=`).length);
}
}
// Add the listeners and variables required to ensure that auto-updating
// happens correctly.
let hasUpdate = false;
let downloadedVersion;
const quitAndInstallWithUpdate = () => {
if (!downloadedVersion) {
return;
}
hasUpdate = true;
autoUpdater.quitAndInstall();
};
// Defines the system-level menu item for manually triggering an update after
const updateAppMenuItem = new MenuItem({
label: 'Update New Expensify',
enabled: false,
click: quitAndInstallWithUpdate,
});
// Actual auto-update listeners
const electronUpdater = browserWindow => ({
init: () => {
autoUpdater.on('update-downloaded', (info) => {
downloadedVersion = info.version;
updateAppMenuItem.enabled = true;
if (browserWindow.isVisible()) {
browserWindow.webContents.send('update-downloaded', info.version);
} else {
quitAndInstallWithUpdate();
}
});
ipcMain.on('start-update', quitAndInstallWithUpdate);
autoUpdater.checkForUpdates();
},
update: () => {
autoUpdater.checkForUpdates();
},
});
const mainWindow = (() => {
const loadURL = isDev
? win => win.loadURL(`http://localhost:${port}`)
: serve({directory: `${__dirname}/../dist`});
// Prod and staging set the icon in the electron-builder config, so only update it here for dev
if (isDev) {
app.dock.setIcon(`${__dirname}/icon-dev.png`);
app.setName('New Expensify');
}
return app.whenReady()
.then(() => {
const browserWindow = new BrowserWindow({
backgroundColor: '#FAFAFA',
width: 1200,
height: 900,
webPreferences: {
nodeIntegration: true,
},
titleBarStyle: 'hidden',
});
// Prod and staging overwrite the app name in the electron-builder config, so only update it here for dev
if (isDev) {
browserWindow.setTitle('New Expensify');
}
// List the Expensify Chat instance under the Window menu, even when it's hidden
const systemMenu = Menu.getApplicationMenu();
systemMenu.insert(4, new MenuItem({
label: 'History',
submenu: [{
role: 'back',
label: 'Back',
accelerator: process.platform === 'darwin' ? 'Cmd+[' : 'Shift+[',
click: () => { browserWindow.webContents.goBack(); },
},
{
role: 'forward',
label: 'Forward',
accelerator: process.platform === 'darwin' ? 'Cmd+]' : 'Shift+]',
click: () => { browserWindow.webContents.goForward(); },
}],
}));
const appMenu = _.find(systemMenu.items, item => item.role === 'appmenu');
appMenu.submenu.insert(1, updateAppMenuItem);
// On mac, pressing cmd++ actually sends a cmd+=. cmd++ is generally the zoom in shortcut, but this is
// not properly listened for by electron. Adding in an invisible cmd+= listener fixes this.
const viewWindow = _.find(systemMenu.items, item => item.role === 'viewmenu');
viewWindow.submenu.append(new MenuItem({
role: 'zoomin',
accelerator: 'CommandOrControl+=',
visible: false,
}));
const windowMenu = _.find(systemMenu.items, item => item.role === 'windowmenu');
windowMenu.submenu.append(new MenuItem({type: 'separator'}));
windowMenu.submenu.append(new MenuItem({
label: 'New Expensify',
accelerator: 'CmdOrCtrl+1',
click: () => browserWindow.show(),
}));
Menu.setApplicationMenu(systemMenu);
// When the user clicks a link that has target="_blank" (which is all external links)
// open the default browser instead of a new electron window
browserWindow.webContents.on('new-window', (e, url) => {
// make sure local urls stay in electron perimeter
if (url.substr(0, 'file://'.length) === 'file://') {
return;
}
// and open every other protocol in the browser
e.preventDefault();
return shell.openExternal(url);
});
// Flag to determine is user is trying to quit the whole application altogether
let quitting = false;
// Closing the chat window should just hide it (vs. fully quitting the application)
browserWindow.on('close', (evt) => {
if (quitting || hasUpdate) {
return;
}
evt.preventDefault();
browserWindow.hide();
});
// Initiating a browser-back or browser-forward with mouse buttons should navigate history.
browserWindow.on('app-command', (e, cmd) => {
if (cmd === 'browser-backward') {
browserWindow.webContents.goBack();
}
if (cmd === 'browser-forward') {
browserWindow.webContents.goForward();
}
});
app.on('before-quit', () => quitting = true);
app.on('activate', () => {
if (expectedUpdateVersion && app.getVersion() !== expectedUpdateVersion) {
return;
}
browserWindow.show();
});
// Hide the app if we expected to upgrade to a new version but never did.
if (expectedUpdateVersion && app.getVersion() !== expectedUpdateVersion) {
browserWindow.hide();
app.hide();
}
ipcMain.on(ELECTRON_EVENTS.REQUEST_VISIBILITY, (event) => {
// This is how synchronous messages work in Electron
// eslint-disable-next-line no-param-reassign
event.returnValue = browserWindow && !browserWindow.isDestroyed() && browserWindow.isFocused();
});
// This allows the renderer process to bring the app
// back into focus if it's minimized or hidden.
ipcMain.on(ELECTRON_EVENTS.REQUEST_FOCUS_APP, () => {
browserWindow.show();
});
// Listen to badge updater event emitted by the render process
// and update the app badge count (MacOS only)
ipcMain.on(ELECTRON_EVENTS.REQUEST_UPDATE_BADGE_COUNT, (event, totalCount) => {
if (totalCount === -1) {
// The electron docs say you should be able to update this and pass no parameters to set the badge
// to a single red dot, but in practice it resulted in an error "TypeError: Insufficient number of
// arguments." - Thus, setting to 1 instead.
// See: https://www.electronjs.org/docs/api/app#appsetbadgecountcount-linux-macos
app.setBadgeCount(1);
} else {
app.setBadgeCount(totalCount);
}
});
return browserWindow;
})
// After initializing and configuring the browser window, load the compiled JavaScript
.then((browserWindow) => {
loadURL(browserWindow);
return browserWindow;
})
// Start checking for JS updates
.then((browserWindow) => {
if (isDev) {
return;
}
checkForUpdates(electronUpdater(browserWindow));
});
});
mainWindow().then(window => window);