From 4e64c8639c7142a9c77069a6ebe5656fbeaec7fa Mon Sep 17 00:00:00 2001 From: COOL Date: Fri, 27 Dec 2024 17:36:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=86=85=E5=AE=B9=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=94=B9=E4=B8=BA=E5=AD=98=E6=96=87=E4=BB=B6=E8=80=8C?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/plugin/entity/info.ts | 12 --- src/modules/plugin/service/center.ts | 6 +- src/modules/plugin/service/info.ts | 111 ++++++++++++++++++++++++--- src/modules/plugin/service/types.ts | 10 ++- 4 files changed, 116 insertions(+), 23 deletions(-) diff --git a/src/modules/plugin/entity/info.ts b/src/modules/plugin/entity/info.ts index 3a7eaeb..2c75d57 100644 --- a/src/modules/plugin/entity/info.ts +++ b/src/modules/plugin/entity/info.ts @@ -36,18 +36,6 @@ export class PluginInfoEntity extends BaseEntity { @Column({ comment: '状态 0-禁用 1-启用', default: 0 }) status: number; - @Column({ comment: '内容', type: 'json' }) - content: { - type: 'comm' | 'module'; - data: string; - }; - - @Column({ comment: 'ts内容', type: 'json' }) - tsContent: { - type: 'ts'; - data: string; - }; - @Column({ comment: '插件的plugin.json', type: 'json', nullable: true }) pluginJson: any; diff --git a/src/modules/plugin/service/center.ts b/src/modules/plugin/service/center.ts index 3098c24..a922a17 100644 --- a/src/modules/plugin/service/center.ts +++ b/src/modules/plugin/service/center.ts @@ -147,7 +147,11 @@ export class PluginCenterService { } const plugins = await this.pluginInfoEntity.findBy(find); for (const plugin of plugins) { - const instance = await this.getInstance(plugin.content.data); + const data = await this.pluginService.getData(plugin.keyName); + if (!data) { + continue; + } + const instance = await this.getInstance(data.content.data); const pluginInfo = { ...plugin.pluginJson, config: this.getConfig(plugin.config), diff --git a/src/modules/plugin/service/info.ts b/src/modules/plugin/service/info.ts index ded4dad..ea977dc 100644 --- a/src/modules/plugin/service/info.ts +++ b/src/modules/plugin/service/info.ts @@ -9,9 +9,11 @@ import { Equal, In, Not, Repository } from 'typeorm'; import { PluginInfoEntity } from '../entity/info'; import { Config, + ILogger, IMidwayApplication, IMidwayContext, InjectClient, + Logger, } from '@midwayjs/core'; import * as _ from 'lodash'; import { PluginInfo } from '../interface'; @@ -23,7 +25,8 @@ import { } from '../event/init'; import { PluginMap, AnyString } from '../../../../typings/plugin'; import { PluginTypesService } from './types'; - +import * as path from 'path'; +import * as fs from 'fs'; /** * 插件信息 */ @@ -53,6 +56,9 @@ export class PluginService extends BaseService { @Inject() pluginTypesService: PluginTypesService; + @Logger() + logger: ILogger; + /** * 新增或更新 * @param param @@ -106,6 +112,8 @@ export class PluginService extends BaseService { const list = await this.pluginInfoEntity.findBy({ id: In(ids) }); for (const item of list) { await this.remove(item.keyName, !!item.hook); + // 删除文件 + await this.deleteData(item.keyName); } await this.pluginInfoEntity.delete(ids); } @@ -318,14 +326,6 @@ export class PluginService extends BaseService { hook: pluginJson.hook, readme, logo, - content: { - type: 'comm', - data: content, - }, - tsContent: { - type: 'ts', - data: tsContent, - }, description: pluginJson.description, pluginJson, config: pluginJson.config, @@ -345,8 +345,101 @@ export class PluginService extends BaseService { // 全新安装 await this.pluginInfoEntity.insert(data); } + // 保存插件内容 + await this.saveData( + { + content: { + type: 'comm', + data: content, + }, + tsContent: { + type: 'ts', + data: tsContent, + }, + }, + pluginJson.key + ); this.pluginTypesService.generateDtsFile(pluginJson.key, tsContent); // 初始化插件 await this.reInit(pluginJson.key); } + + /** + * 将插件内容保存到文件 + * @param content 内容 + * @param keyName 插件key + */ + async saveData( + data: { + content: { + type: 'comm' | 'module'; + data: string; + }; + tsContent: { + type: 'ts'; + data: string; + }; + }, + keyName: string + ) { + const filePath = this.pluginPath(keyName); + // 确保目录存在 + const dir = path.dirname(filePath); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + // 写入文件,如果存在则覆盖 + fs.writeFileSync(filePath, JSON.stringify(data, null, 0), { flag: 'w' }); + } + + /** + * 获得插件数据 + * @param keyName + * @returns + */ + async getData(keyName: string): Promise<{ + content: { + type: 'comm' | 'module'; + data: string; + }; + tsContent: { + type: 'ts'; + data: string; + }; + }> { + const filePath = this.pluginPath(keyName); + if (!fs.existsSync(filePath)) { + this.logger.warn( + `插件[${keyName}]文件不存在,请卸载后重新安装: ${filePath}` + ); + return null; + } + return JSON.parse(await fs.promises.readFile(filePath, 'utf-8')); + } + + /** + * 删除插件 + * @param keyName + */ + async deleteData(keyName: string) { + const filePath = this.pluginPath(keyName); + if (fs.existsSync(filePath)) { + fs.unlinkSync(filePath); + } + } + + /** + * 获得插件路径 + * @param keyName + * @returns + */ + pluginPath(keyName: string) { + return path.join( + this.app.getBaseDir(), + '..', + 'cool', + 'plugin', + `${keyName}.cool` + ); + } } diff --git a/src/modules/plugin/service/types.ts b/src/modules/plugin/service/types.ts index 7551250..59db655 100644 --- a/src/modules/plugin/service/types.ts +++ b/src/modules/plugin/service/types.ts @@ -9,6 +9,7 @@ import { Repository } from 'typeorm'; import * as ts from 'typescript'; import { Utils } from '../../../comm/utils'; import { PluginInfoEntity } from '../entity/info'; +import { PluginService } from './info'; /** * 插件类型服务 @@ -21,6 +22,9 @@ export class PluginTypesService extends BaseService { @InjectEntityModel(PluginInfoEntity) pluginInfoEntity: Repository; + @Inject() + pluginService: PluginService; + @Inject() utils: Utils; @@ -239,7 +243,11 @@ export class PluginTypesService extends BaseService { .select(['a.id', 'a.status', 'a.tsContent', 'a.keyName']) .getMany(); for (const pluginInfo of pluginInfos) { - const tsContent = pluginInfo.tsContent?.data; + const data = await this.pluginService.getData(pluginInfo.keyName); + if (!data) { + continue; + } + const tsContent = data.tsContent?.data; if (tsContent) { await this.generateDtsFile(pluginInfo.keyName, tsContent); await this.utils.sleep(200);