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 ( onlyTheme = 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 ( onlyTheme ) {
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 ( onlyTheme && currentTheme ) {
58+ const pluginIdLower = pluginId . toLowerCase ( ) ;
59+ const currentThemeLower = currentTheme . toLowerCase ( ) ;
60+ let matchFound = false ;
61+ if ( pluginIdLower . includes ( currentThemeLower ) ) {
62+ matchFound = true ;
63+ }
64+
65+ if ( ! matchFound && ! isThemePlugin ( pluginId ) ) {
66+ return ;
67+ }
68+ }
69+
1770 try {
1871 await loadPlugin ( pluginId ) ;
72+ loadedPlugins . add ( pluginId ) ;
1973 results . push ( true ) ;
2074 } catch ( error ) {
21- window . log ( "error" , `Failed to load plugin: ${ pluginId } ` ) ;
22- window . log ( "error" , error ) ;
23- toast ( `Failed to load plugin: ${ pluginId } ` ) ;
75+ console . error ( `Error loading plugin ${ pluginId } :` , error ) ;
2476 failedPlugins . push ( pluginId ) ;
2577 results . push ( false ) ;
2678 }
2779 } ) ;
2880
2981 await Promise . allSettled ( loadPromises ) ;
82+
3083 if ( failedPlugins . length > 0 ) {
3184 setTimeout ( ( ) => {
3285 cleanupFailedPlugins ( failedPlugins ) . catch ( ( error ) => {
@@ -37,6 +90,13 @@ export default async function loadPlugins() {
3790 return results . filter ( Boolean ) . length ;
3891}
3992
93+ function isThemePlugin ( pluginId ) {
94+ // Convert to lowercase for case-insensitive matching
95+ const id = pluginId . toLowerCase ( ) ;
96+ // Check if any theme identifier is present in the plugin ID
97+ return Array . from ( THEME_IDENTIFIERS ) . some ( ( theme ) => id . includes ( theme ) ) ;
98+ }
99+
40100async function cleanupFailedPlugins ( pluginIds ) {
41101 for ( const pluginId of pluginIds ) {
42102 try {
0 commit comments