Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console Log Bridge #655

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/renderer/modules/logger.ts → src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable no-console */

const blurple = "#5865F2";

export type LoggerType =
Expand All @@ -9,7 +7,8 @@ export type LoggerType =
| "Ignition"
| "CommonModules"
| "Components"
| "Manager";
| "Manager"
| "Preload";

const repluggedPrefix = (type: LoggerType, name: string): string => `%c[Replugged:${type}:${name}]`;

Expand Down
13 changes: 11 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { dirname, join } from "path";

import electron from "electron";
import { CONFIG_PATHS } from "src/util.mjs";
import type { RepluggedWebContents } from "../types";
import { RepluggedIpcChannels, type RepluggedWebContents } from "../types";
import { getSetting } from "./ipc/settings";

export const Logger = {
log: console.log,
warn: console.warn,
error: console.error,
};

const electronPath = require.resolve("electron");
const discordPath = join(dirname(require.main!.filename), "..", "app.orig.asar");
const discordPackage = require(join(discordPath, "package.json"));
Expand Down Expand Up @@ -60,6 +65,10 @@ class BrowserWindow extends electron.BrowserWindow {

super(opts);
(this.webContents as RepluggedWebContents).originalPreload = originalPreload;

Logger.log = (...args) => this.webContents.send(RepluggedIpcChannels.CONSOLE_LOG, ...args);
Logger.warn = (...args) => this.webContents.send(RepluggedIpcChannels.CONSOLE_WARN, ...args);
Logger.error = (...args) => this.webContents.send(RepluggedIpcChannels.CONSOLE_ERROR, ...args);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
webFrame,
} from "electron";

import { Logger } from "@logger";

import { RepluggedIpcChannels } from "./types";
// eslint-disable-next-line no-duplicate-imports -- these are only used for types, the other import is for the actual code
import type {
Expand All @@ -17,6 +19,12 @@ import type {
RepluggedTheme,
} from "./types";

const MainLogger = new Logger("Preload", "Backend", "#ea5a5a");

ipcRenderer.on(RepluggedIpcChannels.CONSOLE_LOG, (_event, ...args) => MainLogger.log(...args));
ipcRenderer.on(RepluggedIpcChannels.CONSOLE_WARN, (_event, ...args) => MainLogger.warn(...args));
ipcRenderer.on(RepluggedIpcChannels.CONSOLE_ERROR, (_event, ...args) => MainLogger.error(...args));

let version = "";
void ipcRenderer.invoke(RepluggedIpcChannels.GET_REPLUGGED_VERSION).then((v) => {
version = v;
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/apis/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ApplicationCommandOptionType } from "../../types";
import icon from "../assets/logo.png";
import { constants, i18n, messages, users } from "../modules/common";
import type { Store } from "../modules/common/flux";
import { Logger } from "../modules/logger";
import { Logger } from "@logger";
import { filters, getByStoreName, waitForModule } from "../modules/webpack";
import { t } from "../modules/i18n";

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/contextMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
GetContextItem,
RawContextItem,
} from "../../../types/coremods/contextMenu";
import { Logger } from "../../modules/logger";
import { Logger } from "@logger";

const logger = Logger.api("ContextMenu");

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/devCompanion/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from "../../modules/logger";
import { Logger } from "@logger";
import { getByProps, getBySource, getByValue, getModule } from "src/renderer/modules/webpack";
import { Filter } from "src/types";
import { sourceStrings } from "src/renderer/modules/webpack/patch-load";
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/messagePopover/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getBySource, getFunctionBySource } from "@webpack";
import type { Channel, Message } from "discord-types/general";
import type { GetButtonItem, HoverBarButtonProps } from "../../../types/coremods/message";
import { Logger } from "../../modules/logger";
import { Logger } from "@logger";

const logger = Logger.api("MessagePopover");

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/managers/coremods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { default as contextMenu } from "../coremods/contextMenu/plaintextPatches
import { default as languagePlaintext } from "../coremods/language/plaintextPatches";
import { default as settingsPlaintext } from "../coremods/settings/plaintextPatches";
import { default as badgesPlaintext } from "../coremods/badges/plaintextPatches";
import { Logger } from "../modules/logger";
import { Logger } from "@logger";

const logger = Logger.api("Coremods");

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/managers/ignition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error, log } from "../modules/logger";
import { error, log } from "@logger";
import { ready as commonReady } from "@common";
import { ready as componentsReady } from "../modules/components";
import * as coremods from "./coremods";
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/managers/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// btw, pluginID is the directory name, not the RDNN. We really need a better name for this.
import { loadStyleSheet } from "../util";
import type { PluginExports, RepluggedPlugin } from "../../types";
import { Logger } from "../modules/logger";
import { Logger } from "@logger";
import { patchPlaintext } from "../modules/webpack/plaintext-patch";
import { init } from "../apis/settings";
import type { AddonSettings } from "src/types/addon";
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/managers/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loadStyleSheet } from "../util";
import type { RepluggedTheme } from "../../types";
import type { AddonSettings } from "src/types/addon";
import { init } from "../apis/settings";
import * as logger from "../modules/logger";
import * as logger from "@logger";

const themeElements = new Map<string, HTMLLinkElement>();

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/managers/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AnyAddonManifest, RepluggedEntity } from "src/types/addon";
import notices from "../apis/notices";
import { init } from "../apis/settings";
import { t } from "../modules/i18n";
import { Logger } from "../modules/logger";
import { Logger } from "@logger";
import { waitForProps } from "../modules/webpack";
import * as pluginManager from "./plugins";
import * as themeManager from "./themes";
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error } from "../logger";
import { error } from "@logger";

const modulePromises: Array<() => Promise<void>> = [];

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { intl } from "@common/i18n";
import React from "@common/react";
import { plugins } from "@replugged";
import { t } from "../i18n";
import { Logger } from "../logger";
import { Logger } from "@logger";

import "./ErrorBoundary.css";

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ModuleExports } from "../../../types";
import { error } from "../logger";
import { error } from "@logger";

const modulePromises: Array<() => Promise<void>> = [];

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/webpack/plaintext-patch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PlaintextPatch, RawPlaintextPatch, WebpackModule } from "../../../types";
import { Logger } from "../logger";
import { Logger } from "@logger";

const logger = Logger.api("plaintext-patch");
/**
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/webpack/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from "../logger";
import { Logger } from "@logger";

const logger = Logger.api("webpack");

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/replugged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export { Injector } from "./modules/injector";
* - The "Type" portion of the prefix indicates the type of the message's origin--e.g. an API, a coremod, or a plugin.
* - The "Name" portion of the prefix specifies the origin of the message--the name of the API/coremod/plugin.
*/
export * as logger from "./modules/logger";
export { Logger } from "./modules/logger";
export * as logger from "@logger";
export { Logger } from "@logger";

export * as webpack from "./modules/webpack";
export * as common from "./modules/common";
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export enum RepluggedIpcChannels {
OPEN_QUICKCSS_FOLDER = "REPLUGGED_OPEN_QUICKCSS_FOLDER",
GET_REPLUGGED_VERSION = "REPLUGGED_GET_REPLUGGED_VERSION",
DOWNLOAD_REACT_DEVTOOLS = "REPLUGGED_DOWNLOAD_REACT_DEVTOOLS",
CONSOLE_LOG = "REPLUGGED_CONSOLE_LOG",
CONSOLE_WARN = "REPLUGGED_CONSOLE_WARN",
CONSOLE_ERROR = "REPLUGGED_CONSOLE_ERROR",
}

export interface RepluggedAnnouncement {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"@common": ["src/renderer/modules/common"],
"@common/*": ["src/renderer/modules/common/*"],
"@components": ["src/renderer/modules/components"],
"@components/*": ["src/renderer/modules/components/*"]
"@components/*": ["src/renderer/modules/components/*"],
"@logger": ["src/logger.ts"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
Expand Down
Loading