11import fsOperation from "../fileSystem" ;
22import Url from "../utils/Url" ;
33import loadPlugin from "./loadPlugin" ;
4+ import settings from "./settings" ;
45
5- export default async function loadPlugins ( ) {
6+ // theme-related keywords for determining theme plugins
7+ const THEME_IDENTIFIERS = new Set ( [
8+ "theme" ,
9+ "catppuccin" ,
10+ "pine" ,
11+ "githubdark" ,
12+ "radiant" ,
13+ "rdtheme" ,
14+ "ayumirage" ,
15+ "dust" ,
16+ "synthwave" ,
17+ "dragon" ,
18+ "mint" ,
19+ "monokai" ,
20+ "lumina_code" ,
21+ "sweet" ,
22+ "moonlight" ,
23+ "bluloco" ,
24+ ] ) ;
25+
26+ export default async function loadPlugins ( loadOnlyTheme = false ) {
627 const plugins = await fsOperation ( PLUGIN_DIR ) . lsDir ( ) ;
728 const results = [ ] ;
829 const failedPlugins = [ ] ;
30+ const loadedPlugins = new Set ( ) ;
931
1032 if ( plugins . length > 0 ) {
1133 toast ( strings [ "loading plugins" ] ) ;
1234 }
1335
36+ let pluginsToLoad = [ ] ;
37+ const currentTheme = settings . value . appTheme ;
38+
39+ if ( loadOnlyTheme ) {
40+ // Only load theme plugins matching current theme
41+ pluginsToLoad = plugins . filter ( ( pluginDir ) => {
42+ const pluginId = Url . basename ( pluginDir . url ) ;
43+ return isThemePlugin ( pluginId ) && ! loadedPlugins . has ( pluginId ) ;
44+ } ) ;
45+ } else {
46+ // Load non-theme plugins that aren't loaded yet
47+ pluginsToLoad = plugins . filter ( ( pluginDir ) => {
48+ const pluginId = Url . basename ( pluginDir . url ) ;
49+ return ! isThemePlugin ( pluginId ) && ! loadedPlugins . has ( pluginId ) ;
50+ } ) ;
51+ }
52+
1453 // Load plugins concurrently
15- const loadPromises = plugins . map ( async ( pluginDir ) => {
54+ const loadPromises = pluginsToLoad . map ( async ( pluginDir ) => {
1655 const pluginId = Url . basename ( pluginDir . url ) ;
56+
57+ if ( loadOnlyTheme && currentTheme ) {
58+ const pluginIdLower = pluginId . toLowerCase ( ) ;
59+ const currentThemeLower = currentTheme . toLowerCase ( ) ;
60+ const matchFound = pluginIdLower . includes ( currentThemeLower ) ;
61+ // Skip if:
62+ // 1. No match found with current theme AND
63+ // 2. It's not a theme plugin at all
64+ if ( ! matchFound && ! isThemePlugin ( pluginId ) ) {
65+ return ;
66+ }
67+ }
68+
1769 try {
1870 await loadPlugin ( pluginId ) ;
71+ loadedPlugins . add ( pluginId ) ;
1972 results . push ( true ) ;
2073 } catch ( error ) {
21- window . log ( "error" , `Failed to load plugin: ${ pluginId } ` ) ;
22- window . log ( "error" , error ) ;
23- toast ( `Failed to load plugin: ${ pluginId } ` ) ;
74+ console . error ( `Error loading plugin ${ pluginId } :` , error ) ;
2475 failedPlugins . push ( pluginId ) ;
2576 results . push ( false ) ;
2677 }
2778 } ) ;
2879
2980 await Promise . allSettled ( loadPromises ) ;
81+
3082 if ( failedPlugins . length > 0 ) {
3183 setTimeout ( ( ) => {
3284 cleanupFailedPlugins ( failedPlugins ) . catch ( ( error ) => {
@@ -37,6 +89,13 @@ export default async function loadPlugins() {
3789 return results . filter ( Boolean ) . length ;
3890}
3991
92+ function isThemePlugin ( pluginId ) {
93+ // Convert to lowercase for case-insensitive matching
94+ const id = pluginId . toLowerCase ( ) ;
95+ // Check if any theme identifier is present in the plugin ID
96+ return Array . from ( THEME_IDENTIFIERS ) . some ( ( theme ) => id . includes ( theme ) ) ;
97+ }
98+
4099async function cleanupFailedPlugins ( pluginIds ) {
41100 for ( const pluginId of pluginIds ) {
42101 try {
@@ -45,7 +104,7 @@ async function cleanupFailedPlugins(pluginIds) {
45104 await fsOperation ( pluginDir ) . delete ( ) ;
46105 }
47106 } catch ( error ) {
48- window . log ( " error" , `Failed to cleanup plugin ${ pluginId } :` , error ) ;
107+ console . error ( `Failed to cleanup plugin ${ pluginId } :` , error ) ;
49108 }
50109 }
51110}
0 commit comments