-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.dev.ts
532 lines (488 loc) · 14.8 KB
/
main.dev.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import 'reflect-metadata';
/* eslint global-require: off, no-console: off */
/**
* This module executes inside of electron's main process. You can start
* electron renderer process from here and communicate with the other processes
* through IPC.
*
* When running `yarn build` or `yarn build-main`, this file is compiled to
* `./src/main.prod.js` using webpack. This gives us some performance wins.
*/
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import path from 'path';
import { app, BrowserWindow, dialog, ipcMain, shell } from 'electron';
import mkdirp from 'mkdirp';
import winston from 'winston';
import fs from 'fs';
import MenuBuilder from './menu';
import ApiServer from './api';
import {
ChooseFolderResponseBody,
IpcRequest,
OpenFileLocationRequestBody,
SaveFileRequestBody,
SaveFileResponseBody,
UpdateBuildStatusRequestBody,
} from './ipc';
import Updater from './app/updater';
import WinstonLoggerService from './api/src/logger/WinstonLogger';
import { FirmwareParamsLoaderType } from './api/src/config';
import packageJson from '../package.json';
const logsPath = path.join(app.getPath('userData'), 'logs');
const logsFilename = 'expressslrs-configurator.log';
const winstonLogger = winston.createLogger({
level: 'debug',
format: winston.format.simple(),
defaultMeta: {},
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.prettyPrint(),
winston.format.timestamp()
),
}),
new winston.transports.File({
dirname: logsPath,
filename: logsFilename,
maxFiles: 10,
maxsize: 5_000_000, // in bytes
format: winston.format.combine(
winston.format.prettyPrint(),
winston.format.timestamp()
),
}),
],
});
const logger = new WinstonLoggerService(winstonLogger);
logger.log('path', {
PATH: process.env.PATH,
});
function isASCII(str: string) {
return /^[\x20-\x7F]*$/.test(str);
}
const isWindows = process.platform.startsWith('win');
const isMacOS = process.platform.startsWith('darwin');
let userDataDirectory = app.getPath('userData');
const forceDirtyPath = false;
if (isWindows) {
const dirtyUserDataDirectory = app.isPackaged
? path.join('c:', `.${packageJson.name}`)
: path.join('c:', `.${packageJson.name}-dev`);
try {
if (!isASCII(userDataDirectory) || forceDirtyPath) {
mkdirp.sync(dirtyUserDataDirectory);
userDataDirectory = dirtyUserDataDirectory;
logger.log(
`Non-ASCII path detected, using ${dirtyUserDataDirectory} directory for firmware storage`
);
} else {
logger.log(
`using appdata path ${userDataDirectory} for firmware storage`
);
}
} catch (err) {
logger.error(
'failed to create c:/.expresslrs directory, will use usual path',
undefined,
{
err,
}
);
}
}
// eslint-disable-next-line @typescript-eslint/ban-types
const handleFatalError = (err: Error | object | null | undefined) => {
logger.error(`handling fatal error: ${err}`);
try {
// eslint-disable-next-line promise/no-promise-in-callback
dialog
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.showMessageBox(undefined, {
type: 'error',
buttons: ['Okay'],
title: 'Oops! Something went wrong!',
detail: 'Help us improve your experience by sending an error report',
message: `Error: ${err}`,
})
.then(() => {
console.log('received resp from message box');
process.exit(1);
})
.catch((dialogErr) => {
logger.error('failed to show error dialog', dialogErr.stack);
process.exit(1);
});
} catch (e) {
/*
This API can be called safely before the ready event the app module emits, it is usually used to report errors
in early stage of startup. If called before the app readyevent on Linux, the message will be emitted to stderr,
and no GUI dialog will appear.
*/
dialog.showErrorBox('Oops! Something went wrong!', `Error: ${err}`);
process.exit(1);
}
};
process.on('uncaughtException', (err) => {
logger.error(`uncaughtException ${err.message}`, err.stack);
handleFatalError(err);
});
process.on('unhandledRejection', (err) => {
logger.error(`unhandledRejection: ${err}`);
handleFatalError(err);
});
if (app.commandLine.hasSwitch('disable-gpu')) {
app.disableHardwareAcceleration();
app.commandLine.appendSwitch('disable-software-rasterizer');
}
let mainWindow: BrowserWindow | null = null;
let updater: Updater | null = null;
const localServer: ApiServer = new ApiServer();
if (process.env.NODE_ENV === 'production') {
const sourceMapSupport = require('source-map-support');
sourceMapSupport.install();
}
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
require('electron-debug')({
showDevTools: false,
});
}
const installExtensions = async () => {
const installer = await import('electron-devtools-installer');
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
const extensions = [installer.REACT_DEVELOPER_TOOLS];
return installer
.default(extensions, {
forceDownload,
loadExtensionOptions: {
allowFileAccess: true,
},
})
.catch((err: Error) => {
logger.error(err);
});
};
const createWindow = async () => {
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
await installExtensions();
}
const RESOURCES_PATH = app.isPackaged
? path.join(process.resourcesPath, 'assets')
: path.join(__dirname, '../assets');
const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths);
};
logger.log('trying to get port');
const port = await ApiServer.getPort(3500);
logger.log(`received unused port`, { port });
logger.log('starting server...');
const firmwaresPath = path.join(userDataDirectory, 'firmwares', 'github');
await mkdirp(firmwaresPath);
const targetsStoragePath = path.join(
userDataDirectory,
'firmwares',
'targets'
);
let targetsLoaderType: FirmwareParamsLoaderType =
FirmwareParamsLoaderType.Git;
if (
process.env.FIRMWARE_TARGETS_LOADER_TYPE === FirmwareParamsLoaderType.Git
) {
targetsLoaderType = FirmwareParamsLoaderType.Git;
}
if (
process.env.FIRMWARE_TARGETS_LOADER_TYPE === FirmwareParamsLoaderType.Http
) {
targetsLoaderType = FirmwareParamsLoaderType.Http;
}
const userDefinesStoragePath = path.join(
userDataDirectory,
'firmwares',
'userDefines'
);
let userDefinesLoaderType: FirmwareParamsLoaderType =
FirmwareParamsLoaderType.Git;
if (process.env.USER_DEFINES_LOADER_TYPE === FirmwareParamsLoaderType.Git) {
userDefinesLoaderType = FirmwareParamsLoaderType.Git;
}
if (process.env.USER_DEFINES_LOADER_TYPE === FirmwareParamsLoaderType.Http) {
userDefinesLoaderType = FirmwareParamsLoaderType.Http;
}
const dependenciesPath = app.isPackaged
? path.join(process.resourcesPath, '../dependencies')
: path.join(__dirname, '../dependencies');
const getPlatformioPath = path.join(dependenciesPath, 'get-platformio.py');
const platformioStateTempStoragePath = path.join(
userDataDirectory,
'platformio-temp-state-storage'
);
const localApiServerEnv = process.env;
/* Set the temp directory for the PlatformIO Installer */
localApiServerEnv.PLATFORMIO_INSTALLER_TMPDIR = userDataDirectory;
if (isWindows) {
const publicFolder = 'C:\\Users\\Public';
try {
// As of 2023-04-25, Platform IO installer fails on "building wheel for platformio"
// if the path to the installer temporary directory is too long, so use the Public
// User folder instead of the app data directory
if (fs.existsSync(publicFolder)) {
const testFile = path.join(publicFolder, `${Date.now()}.txt`);
fs.writeFileSync(testFile, '');
fs.unlinkSync(testFile);
localApiServerEnv.PLATFORMIO_INSTALLER_TMPDIR = publicFolder;
logger.log(
`using public folder ${publicFolder} for PlatformIO Installer`
);
} else {
logger.log(
`using appdata path ${userDataDirectory} for PlatformIO Installer`
);
}
} catch (err) {
logger.error(
`${publicFolder} not writable, using ${userDataDirectory} for PlatformIO Installer`,
undefined,
{
err,
}
);
}
}
/*
We manually prepend $PATH on Windows and macOS machines with portable Git and Python locations.
*/
let PATH = process.env.PATH ?? '';
const prependPATH = (pth: string, item: string): string => {
if (pth.indexOf(item) > -1) {
return pth;
}
if (pth.length > 0) {
return `${item}${path.delimiter}${pth}`;
}
return item;
};
if (isWindows) {
const portablePythonLocation = path.join(
dependenciesPath,
'windows_amd64/python'
);
const portableGitLocation = path.join(
dependenciesPath,
'windows_amd64/PortableGit/bin'
);
PATH = prependPATH(PATH, portablePythonLocation);
PATH = prependPATH(PATH, portableGitLocation);
}
if (isMacOS) {
const portablePythonLocation = path.join(
dependenciesPath,
'darwin_amd64/python-portable-darwin-3.8.4/bin'
);
const portableGitLocation = path.join(
dependenciesPath,
'darwin_amd64/git-2.29.2/bin'
);
PATH = prependPATH(PATH, portablePythonLocation);
PATH = prependPATH(PATH, portableGitLocation);
localApiServerEnv.GIT_EXEC_PATH = path.join(
dependenciesPath,
'darwin_amd64/git-2.29.2/libexec/git-core'
);
}
localApiServerEnv.PATH = PATH;
const devicesPath = app.isPackaged
? path.join(process.resourcesPath, 'devices')
: path.join(__dirname, '../devices');
logger.log('local api server PATH', {
PATH,
});
await localServer.start(
{
configuratorGit: {
url: packageJson.repository.url.replaceAll('git+', ''),
owner: packageJson.build.publish.owner,
repositoryName: packageJson.build.publish.repo,
},
multicastDnsSimulatorEnabled:
process.env.MULTICAST_DNS_SIMULATOR_ENABLED === 'true',
firmwaresPath,
getPlatformioPath,
platformioStateTempStoragePath,
PATH,
env: localApiServerEnv,
devicesPath,
targetsStoragePath,
targetsLoader: targetsLoaderType,
userDefinesLoader: userDefinesLoaderType,
userDefinesStoragePath,
},
logger,
port
);
logger.log('server started');
mainWindow = new BrowserWindow({
show: false,
width: 1400,
height: 920,
icon: getAssetPath('icon.png'),
// TODO: improve electron.js security
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
mainWindow.on('close', (e) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (buildInProgress) {
const choice = dialog.showMessageBoxSync(mainWindow!, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Are you sure you want to quit?',
message: 'It looks like you have a build in progress',
});
if (choice === 1) {
e.preventDefault();
}
}
});
const apiUrl = `http://localhost:${port}/graphql`;
const subscriptionsUrl = `ws://localhost:${port}/graphql`;
mainWindow.loadURL(
`file://${__dirname}/index.html?api_url=${apiUrl}&subscriptions_url=${subscriptionsUrl}`
);
// TODO: Use 'ready-to-show' event
// https://github.com/electron/electron/blob/master/docs/api/browser-window.md#using-ready-to-show-event
mainWindow.webContents.on('did-finish-load', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
}
if (process.env.START_MINIMIZED) {
mainWindow.minimize();
} else {
mainWindow.show();
mainWindow.focus();
}
// set the window title based on package.json
const windowTitle = require('./package.json').productName;
mainWindow.setTitle(windowTitle);
});
mainWindow.on('closed', () => {
mainWindow = null;
});
// mainWindow.setMenuBarVisibility(false);
const menuBuilder = new MenuBuilder(mainWindow);
menuBuilder.buildMenu();
// Open urls in the user's browser
mainWindow.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
});
updater = new Updater(logger, mainWindow);
};
/**
* Add event listeners...
*/
app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
if (process.platform !== 'darwin') {
localServer.stop();
app.quit();
}
});
app
.whenReady()
.then(createWindow)
.catch((err: Error) => {
logger.error(`createWindow error ${err}`);
handleFatalError(err);
})
.then(() => {
return updater?.checkForUpdates();
})
.catch((err: Error) => {
logger.error(`Auto update error ${err}`);
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
/*
Handle IPC requests from the User Interface
*/
ipcMain.on(
IpcRequest.OpenFileLocation,
(_, arg: OpenFileLocationRequestBody) => {
logger.log('received a request to show item in folder', {
path: arg.path,
});
shell.showItemInFolder(arg.path);
}
);
ipcMain.handle(
IpcRequest.ChooseFolder,
async (): Promise<ChooseFolderResponseBody> => {
const result = await dialog.showOpenDialog({
title: 'Select firmware source folder',
message: 'Folder must contain platformio.ini file',
properties: ['openDirectory'],
});
if (result.canceled || result.filePaths.length === 0) {
return {
success: false,
directoryPath: '',
};
}
return {
success: true,
directoryPath: result.filePaths[0],
};
}
);
ipcMain.on(IpcRequest.OpenLogsFolder, () => {
const logsLocation = path.join(logsPath, logsFilename);
logger.log('received a request to logs path', {
logsLocation,
});
shell.showItemInFolder(logsLocation);
});
let buildInProgress = false;
ipcMain.on(
IpcRequest.UpdateBuildStatus,
(_, arg: UpdateBuildStatusRequestBody) => {
buildInProgress = arg.buildInProgress;
logger.log('received a request to update build status', {
arg,
});
}
);
ipcMain.handle(
IpcRequest.SaveFile,
async (_, arg: SaveFileRequestBody): Promise<SaveFileResponseBody> => {
const result = await dialog.showSaveDialog({
title: 'Save File',
defaultPath: arg.defaultPath,
});
if (result.canceled || !result.filePath || result.filePath.length === 0) {
return {
success: false,
path: '',
};
}
await fs.promises.writeFile(result.filePath, arg.data);
return {
success: true,
path: result.filePath,
};
}
);