Skip to content

Commit

Permalink
refactor: move utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdvlpr committed Dec 6, 2024
1 parent a71717e commit 592c157
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src-electron/electron-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import { join } from 'upath';

import { PLATFORM } from './constants';
import { cancelAllDownloads } from './main/downloads';
import { captureElectronError } from './main/log';
import { initScreenListeners } from './main/screen';
import { initSessionListeners } from './main/session';
import { initUpdater } from './main/updater';
import { captureElectronError } from './main/utils';
import { closeOtherWindows, sendToWindow } from './main/window/window-base';
import {
authorizedClose,
Expand Down
3 changes: 1 addition & 2 deletions src-electron/main/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { ElectronDownloadManager } from 'electron-dl-manager';
import { ensureDir } from 'fs-extra';
import { basename } from 'upath';

import { fetchJson } from './../utils';
import { captureElectronError } from './log';
import { captureElectronError, fetchJson } from './utils';
import { sendToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'src/constants/media';
import { basename, dirname, join, toUnix } from 'upath';

import { captureElectronError } from './log';
import { captureElectronError } from './utils';
import { sendToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from 'electron';

import { PLATFORM } from '../constants';
import { getAppVersion, isSelf } from './../utils';
import { downloadFile } from './downloads';
import {
openFileDialog,
Expand All @@ -36,6 +35,7 @@ import {
unregisterAllShortcuts,
unregisterShortcut,
} from './shortcuts';
import { getAppVersion, isSelf } from './utils';
import { logToWindow } from './window/window-base';
import { mainWindow, toggleAuthorizedClose } from './window/window-main';
import { mediaWindow, moveMediaWindow } from './window/window-media';
Expand Down
26 changes: 0 additions & 26 deletions src-electron/main/log.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src-electron/main/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Display, ScreenPreferences } from 'src/types';

import { app, type BrowserWindow, screen } from 'electron';

import { captureElectronError } from './log';
import { captureElectronError } from './utils';
import { mainWindow } from './window/window-main';
import { mediaWindow, moveMediaWindow } from './window/window-media';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/security.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app, session, shell } from 'electron';

import { isSelf, isTrustedDomain } from './../utils';
import { isSelf, isTrustedDomain } from './utils';
import { logToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isSelf,
isTrustedDomain,
isValidUrl,
} from './../utils';
} from './utils';

export let urlVariables: undefined | UrlVariables;

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SettingsValues } from 'src/types';

import { globalShortcut } from 'electron';

import { captureElectronError } from './log';
import { captureElectronError } from './utils';
import { sendToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { exists } from 'fs-extra';
import { join } from 'path';

import { PLATFORM } from './../constants';
import { captureElectronError } from './log';
import { captureElectronError } from './utils';

export async function initUpdater() {
autoUpdater.on('error', (error, message) => {
Expand Down
29 changes: 26 additions & 3 deletions src-electron/utils.ts → src-electron/main/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ExclusiveEventHintOrCaptureContext } from '@sentry/core/build/types/utils/prepareEvent';

import { captureException } from '@sentry/electron/main';
import { version } from 'app/package.json';
import { app } from 'electron';

import { IS_DEV, JW_DOMAINS, TRUSTED_DOMAINS } from './constants';
import { captureElectronError } from './main/log';
import { urlVariables } from './main/session';
import { IS_DEV, JW_DOMAINS, TRUSTED_DOMAINS } from './../constants';
import { urlVariables } from './session';

/**
* Gets the current app version
Expand Down Expand Up @@ -154,6 +156,27 @@ export const fetchJson = async <T>(
return null;
};

/**
* Logs an error to the console or to Sentry
* @param error The error to log
* @param context The context to log with the error
*/
export function captureElectronError(
error: Error | string | unknown,
context?: ExclusiveEventHintOrCaptureContext,
) {
if (error instanceof Error && error.cause) {
captureElectronError(error.cause, context);
}

if (IS_DEV) {
console.error(error);
console.warn('context', context);
} else {
captureException(error, context);
}
}

/**
* Throttles a function to only run once every `delay` milliseconds
* @param func The function to throttle
Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/window/window-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
} from 'electron';
import { join, resolve } from 'path';

import { captureElectronError } from './../log';
import { urlVariables } from './../session';
import { captureElectronError } from './../utils';
import { StatefulBrowserWindow } from './window-state';

export function closeOtherWindows(source: BrowserWindow) {
Expand Down
4 changes: 2 additions & 2 deletions src-electron/main/window/window-main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { BrowserWindow } from 'electron';

import { PLATFORM } from 'app/src-electron/constants';
import { throttle } from 'app/src-electron/utils';

import { cancelAllDownloads } from '../downloads';
import { cancelAllDownloads } from './../downloads';
import { throttle } from './../utils';
import { closeOtherWindows, createWindow, sendToWindow } from './window-base';
import { createMediaWindow, moveMediaWindow } from './window-media';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/window/window-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { ScreenPreferences } from 'src/types';
import { HD_RESOLUTION, PLATFORM } from 'app/src-electron/constants';
import { join, resolve } from 'path';

import { captureElectronError } from './../log';
import { getAllScreens, getWindowScreen, screenPreferences } from './../screen';
import { captureElectronError } from './../utils';
import { createWindow, sendToWindow } from './window-base';
import { mainWindow } from './window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/window/window-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { ensureDirSync, readJsonSync, writeJsonSync } from 'fs-extra';
import { dirname, join } from 'path';

import { captureElectronError } from './../log';
import { captureElectronError } from './../utils';

interface ExtraOptions {
/** The name of file. Defaults to `window-state.json`. */
Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/window/window-website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { NavigateWebsiteAction } from 'src/types';
import { HD_RESOLUTION, PLATFORM } from 'app/src-electron/constants';
import { type BrowserWindow, systemPreferences, type Video } from 'electron';

import { captureElectronError } from './../log';
import { captureElectronError } from './../utils';
import { createWindow, logToWindow, sendToWindow } from './window-base';
import { mainWindow } from './window-main';

Expand Down

0 comments on commit 592c157

Please sign in to comment.