()
| 11 | * 在应用启动时调用,自动将内置插件添加到数据库 |
| 12 | */ |
| 13 | export function loadInternalPlugins(): void { |
| 14 | console.log('[InternalPlugin] 开始加载内置插件...') |
| 15 | |
| 16 | const isDev = !app.isPackaged |
| 17 | const existingPlugins = api.dbGet('plugins') || [] |
| 18 | |
| 19 | // 移除旧的内置插件记录(基于名称判断) |
| 20 | const filteredPlugins = existingPlugins.filter( |
| 21 | (p: any) => !BUNDLED_INTERNAL_PLUGIN_NAMES.includes(p.name) |
| 22 | ) |
| 23 | |
| 24 | // 重新加载所有内置插件 |
| 25 | for (const pluginName of BUNDLED_INTERNAL_PLUGIN_NAMES) { |
| 26 | try { |
| 27 | const pluginPath = getInternalPluginPath(pluginName) |
| 28 | |
| 29 | // 开发模式:插件路径直接指向 public 目录 |
| 30 | // 生产模式:插件路径指向插件根目录(打包时已将 dist 构建产物复制到此目录) |
| 31 | const effectivePluginPath = isDev ? path.join(pluginPath, 'public') : pluginPath |
| 32 | |
| 33 | // 读取 plugin.json |
| 34 | const pluginJsonPath = path.join(effectivePluginPath, 'plugin.json') |
| 35 | |
| 36 | if (!fsSync.existsSync(pluginJsonPath)) { |
| 37 | console.error( |
| 38 | `[InternalPlugin] 内置插件 ${pluginName} 的 plugin.json 不存在:`, |
| 39 | pluginJsonPath |
| 40 | ) |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | const pluginConfig = JSON.parse(fsSync.readFileSync(pluginJsonPath, 'utf-8')) |
| 45 | |
| 46 | // 构建插件信息 |
| 47 | const logoPath = pluginConfig.logo ? path.join(effectivePluginPath, pluginConfig.logo) : '' |
| 48 | |
| 49 | // 生产环境且 server 已启动时,使用 HTTP URL 加载插件(避免 file:// 下的 CSP 限制) |
| 50 | const serverPort = getInternalPluginServerPort() |
| 51 | const mainPath = pluginConfig.main |
| 52 | ? serverPort > 0 |
| 53 | ? getInternalPluginUrl(pluginName, pluginConfig.main) |
| 54 | : path.join(effectivePluginPath, pluginConfig.main) |
| 55 | : undefined |
| 56 | |
| 57 | const pluginInfo = { |
| 58 | name: pluginConfig.name, |
| 59 | title: pluginConfig.title, |
| 60 | version: pluginConfig.version, |
| 61 | description: pluginConfig.description || '', |
| 62 | logo: logoPath ? pathToFileURL(logoPath).href : '', |
| 63 | path: effectivePluginPath, // 保存有效路径(开发模式下是 public 目录) |
| 64 | features: pluginConfig.features || [], |
| 65 | isDevelopment: isDev, |
| 66 | main: mainPath |
| 67 | } |
| 68 | console.log('[InternalPlugin] 加载插件', pluginInfo) |
| 69 | |
| 70 | filteredPlugins.push(pluginInfo) |
no test coverage detected