mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-11 16:52:49 +00:00
插件内容数据改为存文件而不是数据库
This commit is contained in:
parent
5ca76f1ac8
commit
4e64c8639c
@ -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;
|
||||
|
||||
|
||||
@ -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),
|
||||
|
||||
@ -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`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user