forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathloadPlugins.js
More file actions
110 lines (98 loc) · 2.84 KB
/
loadPlugins.js
File metadata and controls
110 lines (98 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fsOperation from "../fileSystem";
import Url from "../utils/Url";
import loadPlugin from "./loadPlugin";
import settings from "./settings";
// theme-related keywords for determining theme plugins
const THEME_IDENTIFIERS = new Set([
"theme",
"catppuccin",
"pine",
"githubdark",
"radiant",
"rdtheme",
"ayumirage",
"dust",
"synthwave",
"dragon",
"mint",
"monokai",
"lumina_code",
"sweet",
"moonlight",
"bluloco",
]);
export default async function loadPlugins(onlyTheme = false) {
const plugins = await fsOperation(PLUGIN_DIR).lsDir();
const results = [];
const failedPlugins = [];
const loadedPlugins = new Set();
if (plugins.length > 0) {
toast(strings["loading plugins"]);
}
let pluginsToLoad = [];
const currentTheme = settings.value.appTheme;
if (onlyTheme) {
// Only load theme plugins matching current theme
pluginsToLoad = plugins.filter((pluginDir) => {
const pluginId = Url.basename(pluginDir.url);
return isThemePlugin(pluginId) && !loadedPlugins.has(pluginId);
});
} else {
// Load non-theme plugins that aren't loaded yet
pluginsToLoad = plugins.filter((pluginDir) => {
const pluginId = Url.basename(pluginDir.url);
return !isThemePlugin(pluginId) && !loadedPlugins.has(pluginId);
});
}
// Load plugins concurrently
const loadPromises = pluginsToLoad.map(async (pluginDir) => {
const pluginId = Url.basename(pluginDir.url);
if (onlyTheme && currentTheme) {
const pluginIdLower = pluginId.toLowerCase();
const currentThemeLower = currentTheme.toLowerCase();
const matchFound = pluginIdLower.includes(currentThemeLower);
// Skip if:
// 1. No match found with current theme AND
// 2. It's not a theme plugin at all
if (!matchFound && !isThemePlugin(pluginId)) {
return;
}
}
try {
await loadPlugin(pluginId);
loadedPlugins.add(pluginId);
results.push(true);
} catch (error) {
console.error(`Error loading plugin ${pluginId}:`, error);
failedPlugins.push(pluginId);
results.push(false);
}
});
await Promise.allSettled(loadPromises);
if (failedPlugins.length > 0) {
setTimeout(() => {
cleanupFailedPlugins(failedPlugins).catch((error) => {
console.error("Failed to cleanup plugins:", error);
});
}, 1000);
}
return results.filter(Boolean).length;
}
function isThemePlugin(pluginId) {
// Convert to lowercase for case-insensitive matching
const id = pluginId.toLowerCase();
// Check if any theme identifier is present in the plugin ID
return Array.from(THEME_IDENTIFIERS).some((theme) => id.includes(theme));
}
async function cleanupFailedPlugins(pluginIds) {
for (const pluginId of pluginIds) {
try {
const pluginDir = Url.join(PLUGIN_DIR, pluginId);
if (await fsOperation(pluginDir).exists()) {
await fsOperation(pluginDir).delete();
}
} catch (error) {
window.log("error", `Failed to cleanup plugin ${pluginId}:`, error);
}
}
}