From 7cd4d2ba10106c10602c86f74f55743ded588896 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Jul 2019 11:45:19 +0200 Subject: WIP plugins: add theme support --- server/lib/plugins/plugin-manager.ts | 34 ++++++++++++++++++++++++++++++---- server/lib/plugins/theme-utils.ts | 24 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 server/lib/plugins/theme-utils.ts (limited to 'server/lib') diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 7cbfa8569..8496979f8 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -11,6 +11,7 @@ import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' import { PluginType } from '../../../shared/models/plugins/plugin.type' import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' import { outputFile } from 'fs-extra' +import { ServerConfigPlugin } from '../../../shared/models/server' export interface RegisteredPlugin { name: string @@ -47,7 +48,7 @@ export class PluginManager { private constructor () { } - async registerPlugins () { + async registerPluginsAndThemes () { await this.resetCSSGlobalFile() const plugins = await PluginModel.listEnabledPluginsAndThemes() @@ -63,12 +64,20 @@ export class PluginManager { this.sortHooksByPriority() } + getRegisteredPluginOrTheme (name: string) { + return this.registeredPlugins[name] + } + getRegisteredPlugin (name: string) { - return this.registeredPlugins[ name ] + const registered = this.getRegisteredPluginOrTheme(name) + + if (!registered || registered.type !== PluginType.PLUGIN) return undefined + + return registered } getRegisteredTheme (name: string) { - const registered = this.getRegisteredPlugin(name) + const registered = this.getRegisteredPluginOrTheme(name) if (!registered || registered.type !== PluginType.THEME) return undefined @@ -76,7 +85,11 @@ export class PluginManager { } getRegisteredPlugins () { - return this.registeredPlugins + return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN) + } + + getRegisteredThemes () { + return this.getRegisteredPluginsOrThemes(PluginType.THEME) } async runHook (hookName: string, param?: any) { @@ -309,6 +322,19 @@ export class PluginManager { } } + private getRegisteredPluginsOrThemes (type: PluginType) { + const plugins: RegisteredPlugin[] = [] + + for (const pluginName of Object.keys(this.registeredPlugins)) { + const plugin = this.registeredPlugins[ pluginName ] + if (plugin.type !== type) continue + + plugins.push(plugin) + } + + return plugins + } + static get Instance () { return this.instance || (this.instance = new this()) } diff --git a/server/lib/plugins/theme-utils.ts b/server/lib/plugins/theme-utils.ts new file mode 100644 index 000000000..066339e65 --- /dev/null +++ b/server/lib/plugins/theme-utils.ts @@ -0,0 +1,24 @@ +import { DEFAULT_THEME } from '../../initializers/constants' +import { PluginManager } from './plugin-manager' +import { CONFIG } from '../../initializers/config' + +function getThemeOrDefault (name: string) { + if (isThemeRegistered(name)) return name + + // Fallback to admin default theme + if (name !== CONFIG.THEME.DEFAULT) return getThemeOrDefault(CONFIG.THEME.DEFAULT) + + return DEFAULT_THEME +} + +function isThemeRegistered (name: string) { + if (name === DEFAULT_THEME) return true + + return !!PluginManager.Instance.getRegisteredThemes() + .find(r => r.name === name) +} + +export { + getThemeOrDefault, + isThemeRegistered +} -- cgit v1.2.3