-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathmain.ts
More file actions
103 lines (90 loc) · 3.18 KB
/
main.ts
File metadata and controls
103 lines (90 loc) · 3.18 KB
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
/* eslint-disable unicorn/prefer-top-level-await */
import dotenv from 'dotenv';
import { app, shell } from 'electron';
import { LevelOption } from 'electron-log';
import log from 'electron-log/main';
import { LogFile } from './constants';
import { DesktopApp } from './desktopApp';
import { removeAnsiCodesTransform, replaceFileLoggingTransform } from './infrastructure/structuredLogging';
import { initializeAppState } from './main-process/appState';
import { DevOverrides } from './main-process/devOverrides';
import SentryLogging from './services/sentry';
import { getTelemetry } from './services/telemetry';
import { DesktopConfig } from './store/desktopConfig';
import { rotateLogFiles } from './utils';
// Synchronous pre-start configuration
dotenv.config();
initalizeLogging();
const telemetry = getTelemetry();
initializeAppState();
const overrides = new DevOverrides();
// Register the quit handlers regardless of single instance lock and before squirrel startup events.
quitWhenAllWindowsAreClosed();
trackAppQuitEvents();
initializeSentry();
// Async config & app start
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
log.info('App already running. Exiting...');
app.quit();
} else {
startApp().catch((error) => {
log.error('Unhandled exception in app startup', error);
app.exit(2020);
});
}
/** Wrapper for top-level await; the app is bundled to CommonJS. */
async function startApp() {
// Wait for electron app ready event
await new Promise<void>((resolve) => app.once('ready', () => resolve()));
await rotateLogFiles(app.getPath('logs'), LogFile.Main, 50);
log.debug('App ready');
telemetry.registerHandlers();
telemetry.track('desktop:app_ready');
// Load config or exit
const config = await DesktopConfig.load(shell);
if (!config) {
DesktopApp.fatalError({
message: 'Unknown error loading app config on startup.',
title: 'User Data',
exitCode: 20,
});
}
const desktopApp = new DesktopApp(overrides, config);
await desktopApp.showLoadingPage();
await desktopApp.start();
}
/**
* Must be called prior to any logging. Sets default log level and logs app version.
* Corrects issues when logging structured data (to file).
*/
function initalizeLogging() {
log.initialize();
log.transports.file.level = (process.env.LOG_LEVEL as LevelOption) ?? 'info';
log.transports.file.transforms.unshift(removeAnsiCodesTransform);
replaceFileLoggingTransform(log.transports);
// Set the app version for the desktop app. Relied on by Manager and other sub-processes.
process.env.__COMFYUI_DESKTOP_VERSION__ = app.getVersion();
log.info(`Starting app v${app.getVersion()}`);
}
/** Quit when all windows are closed.*/
function quitWhenAllWindowsAreClosed() {
app.on('window-all-closed', () => {
log.info('Quitting ComfyUI because window all closed');
app.quit();
});
}
/** Add telemetry for the app quit event. */
function trackAppQuitEvents() {
app.on('quit', (event, exitCode) => {
telemetry.track('desktop:app_quit', {
reason: event,
exitCode,
});
});
}
/** Sentry needs to be initialized at the top level. */
function initializeSentry() {
log.verbose('Initializing Sentry');
SentryLogging.init();
}