Skip to content

Commit

Permalink
feat: rename plugins to clarify context (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyBrick authored Nov 9, 2023
1 parent 47cccbc commit 06dc0e8
Show file tree
Hide file tree
Showing 58 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
const commonConfig: UserConfig = {
plugins: [
viteResolve({
'virtual:MainPlugins': pluginVirtualModuleGenerator('back'),
'virtual:MainPlugins': pluginVirtualModuleGenerator('main'),
'virtual:MenuPlugins': pluginVirtualModuleGenerator('menu'),
}),
],
Expand Down Expand Up @@ -85,7 +85,7 @@ export default defineConfig({
const commonConfig: UserConfig = {
plugins: [
viteResolve({
'virtual:RendererPlugins': pluginVirtualModuleGenerator('front'),
'virtual:RendererPlugins': pluginVirtualModuleGenerator('renderer'),
}),
],
root: './src/',
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Create a folder in `plugins/YOUR-PLUGIN-NAME`:
- if you need to manipulate the BrowserWindow, create a file with the following template:

```typescript
// file: back.ts
// file: main.ts
export default (win: Electron.BrowserWindow, config: ConfigType<'YOUR-PLUGIN-NAME'>) => {
// something
};
Expand All @@ -229,7 +229,7 @@ const mainPlugins = {
- if you need to change the front, create a file with the following template:

```typescript
// file: front.ts
// file: renderer.ts
export default (config: ConfigType<'YOUR-PLUGIN-NAME'>) => {
// This function will be called as a preload script
// So you can use front features like `document.querySelector`
Expand Down Expand Up @@ -269,7 +269,7 @@ export default {
import path from 'node:path';
import style from './style.css';

// back.ts
// main.ts
export default (win: Electron.BrowserWindow) => {
injectCSS(win.webContents, style);
};
Expand All @@ -278,7 +278,7 @@ export default (win: Electron.BrowserWindow) => {
- changing the HTML:

```typescript
// front.ts
// renderer.ts
export default () => {
// Remove the login button
document.querySelector(".sign-in-link.ytmusic-nav-bar").remove();
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { restart, setupAppControls } from './providers/app-controls';
import { APP_PROTOCOL, handleProtocol, setupProtocolHandler } from './providers/protocol-handler';

// eslint-disable-next-line import/order
import { pluginList as mainPluginList } from 'virtual:MainPlugins';
import { mainPlugins } from 'virtual:MainPlugins';

import { setOptions as pipSetOptions } from './plugins/picture-in-picture/back';
import { setOptions as pipSetOptions } from './plugins/picture-in-picture/main';

import youtubeMusicCSS from './youtube-music.css';

Expand Down Expand Up @@ -83,18 +83,18 @@ function onClosed() {
mainWindow = null;
}

export const mainPluginNames = Object.keys(mainPluginList);
export const mainPluginNames = Object.keys(mainPlugins);

if (is.windows()) {
delete mainPluginList['touchbar'];
delete mainPlugins['touchbar'];
} else if (is.macOS()) {
delete mainPluginList['taskbar-mediacontrol'];
delete mainPlugins['taskbar-mediacontrol'];
} else {
delete mainPluginList['touchbar'];
delete mainPluginList['taskbar-mediacontrol'];
delete mainPlugins['touchbar'];
delete mainPlugins['taskbar-mediacontrol'];
}

ipcMain.handle('get-main-plugin-names', () => Object.keys(mainPluginList));
ipcMain.handle('get-main-plugin-names', () => Object.keys(mainPlugins));

async function loadPlugins(win: BrowserWindow) {
injectCSS(win.webContents, youtubeMusicCSS);
Expand Down Expand Up @@ -123,9 +123,9 @@ async function loadPlugins(win: BrowserWindow) {

for (const [plugin, options] of config.plugins.getEnabled()) {
try {
if (Object.hasOwn(mainPluginList, plugin)) {
if (Object.hasOwn(mainPlugins, plugin)) {
console.log('Loaded plugin - ' + plugin);
const handler = mainPluginList[plugin as keyof typeof mainPluginList];
const handler = mainPlugins[plugin as keyof typeof mainPlugins];
if (handler) {
await handler(win, options as never);
}
Expand Down
2 changes: 1 addition & 1 deletion src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { startingPages } from './providers/extracted-data';
import promptOptions from './providers/prompt-options';

// eslint-disable-next-line import/order
import { pluginList as menuList } from 'virtual:MenuPlugins';
import { menuPlugins as menuList } from 'virtual:MenuPlugins';

import { getAvailablePluginNames } from './plugins/utils/main';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plugins/discord/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import prompt from 'custom-electron-prompt';

import { clear, connect, isConnected, registerRefresh } from './back';
import { clear, connect, isConnected, registerRefresh } from './main';

import { setMenuOptions } from '../../config/plugins';
import promptOptions from '../../providers/prompt-options';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { YoutubeFormatList, type Preset, DefaultPresetList } from './types';

import style from './style.css';

import { fetchFromGenius } from '../lyrics-genius/back';
import { fetchFromGenius } from '../lyrics-genius/main';
import { isEnabled } from '../../config/plugins';
import { cleanupName, getImage, SongInfo } from '../../providers/song-info';
import { injectCSS } from '../utils/main';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/downloader/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dialog } from 'electron';

import { downloadPlaylist } from './back';
import { downloadPlaylist } from './main';
import { defaultMenuDownloadLabel, getFolder } from './utils';
import { DefaultPresetList } from './types';
import config from './config';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plugins/lyrics-genius/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BrowserWindow, MenuItem } from 'electron';

import { LyricGeniusType, toggleRomanized } from './back';
import { LyricGeniusType, toggleRomanized } from './main';

import { setOptions } from '../../config/plugins';
import { MenuTemplate } from '../../menu';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plugins/picture-in-picture/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import prompt from 'custom-electron-prompt';

import { BrowserWindow } from 'electron';

import { setOptions } from './back';
import { setOptions } from './main';

import promptOptions from '../../providers/prompt-options';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plugins/precise-volume/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import prompt, { KeybindOptions } from 'custom-electron-prompt';

import { BrowserWindow, MenuItem } from 'electron';

import { enabled } from './back';
import { enabled } from './main';

import { setMenuOptions } from '../../config/plugins';
import promptOptions from '../../providers/prompt-options';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import buttonTemplate from './templates/button_template.html?raw';

import { ElementFromHtml } from '../utils/renderer';

import { moveVolumeHud as preciseVolumeMoveVolumeHud } from '../precise-volume/front';
import { moveVolumeHud as preciseVolumeMoveVolumeHud } from '../precise-volume/renderer';

import { YoutubePlayer } from '../../types/youtube-player';
import { ThumbnailElement } from '../../types/get-player-response';
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import is from 'electron-is';
import config from './config';

// eslint-disable-next-line import/order
import { pluginList as preloadPluginList } from 'virtual:PreloadPlugins';
import { preloadPlugins } from 'virtual:PreloadPlugins';

import type { ConfigType, OneOfDefaultConfigKey } from './config/dynamic';

Expand All @@ -19,8 +19,8 @@ export type PluginMapper<Type extends 'renderer' | 'preload' | 'backend'> = {
const enabledPluginNameAndOptions = config.plugins.getEnabled();

enabledPluginNameAndOptions.forEach(async ([plugin, options]) => {
if (Object.hasOwn(preloadPluginList, plugin)) {
const handler = preloadPluginList[plugin];
if (Object.hasOwn(preloadPlugins, plugin)) {
const handler = preloadPlugins[plugin];
try {
await handler?.(options);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setupSongControls } from './providers/song-controls-front';
import { startingPages } from './providers/extracted-data';

// eslint-disable-next-line import/order
import { pluginList as rendererPluginList } from 'virtual:RendererPlugins';
import { rendererPlugins } from 'virtual:RendererPlugins';

const enabledPluginNameAndOptions = window.mainConfig.plugins.getEnabled();

Expand Down Expand Up @@ -93,8 +93,8 @@ function onApiLoaded() {

(() => {
enabledPluginNameAndOptions.forEach(async ([pluginName, options]) => {
if (Object.hasOwn(rendererPluginList, pluginName)) {
const handler = rendererPluginList[pluginName];
if (Object.hasOwn(rendererPlugins, pluginName)) {
const handler = rendererPlugins[pluginName];
try {
await handler?.(options as never);
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions src/virtual-module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ declare module 'virtual:MainPlugins' {
import type { BrowserWindow } from 'electron';
import type { ConfigType } from './config/dynamic';

export const pluginList: Record<string, (win: BrowserWindow, options: ConfigType) => Promise<void>>;
export const mainPlugins: Record<string, (win: BrowserWindow, options: ConfigType) => Promise<void>>;
}

declare module 'virtual:MenuPlugins' {
import type { BrowserWindow } from 'electron';
import type { MenuTemplate } from './menu';
import type { ConfigType } from './config/dynamic';

export const pluginList: Record<string, (win: BrowserWindow, options: ConfigType, refreshMenu: () => void) => MenuTemplate>;
export const menuPlugins: Record<string, (win: BrowserWindow, options: ConfigType, refreshMenu: () => void) => MenuTemplate>;
}

declare module 'virtual:PreloadPlugins' {
import type { ConfigType } from './config/dynamic';

export const pluginList: Record<string, (options: ConfigType) => Promise<void>>;
export const preloadPlugins: Record<string, (options: ConfigType) => Promise<void>>;
}

declare module 'virtual:RendererPlugins' {
import type { ConfigType } from './config/dynamic';

export const pluginList: Record<string, (options: ConfigType) => Promise<void>>;
export const rendererPlugins: Record<string, (options: ConfigType) => Promise<void>>;
}
4 changes: 2 additions & 2 deletions vite-plugins/plugin-virtual-module-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { globSync } from 'glob';

const snakeToCamel = (text: string) => text.replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase());

export const pluginVirtualModuleGenerator = (mode: 'back' | 'preload' | 'front' | 'menu') => {
export const pluginVirtualModuleGenerator = (mode: 'main' | 'preload' | 'renderer' | 'menu') => {
const srcPath = resolve(__dirname, '..', 'src');

const plugins = globSync(`${srcPath}/plugins/*`)
Expand All @@ -18,7 +18,7 @@ export const pluginVirtualModuleGenerator = (mode: 'back' | 'preload' | 'front'
result += `import ${snakeToCamel(name)}Plugin from "./${relative(resolve(srcPath, '..'), path).replace(/\\/g, '/')}/${mode}";\n`;
}

result += 'export const pluginList = {\n';
result += `export const ${mode}Plugins = {\n`;
for (const { name } of plugins) {
result += ` "${name}": ${snakeToCamel(name)}Plugin,\n`;
}
Expand Down

0 comments on commit 06dc0e8

Please sign in to comment.