插件内容数据改为存文件而不是数据库

This commit is contained in:
COOL 2024-12-27 17:36:27 +08:00
parent 5ca76f1ac8
commit 4e64c8639c
4 changed files with 116 additions and 23 deletions

View File

@ -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;

View File

@ -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),

View File

@ -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`
);
}
}

View File

@ -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<PluginInfoEntity>;
@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);