mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-13 18:42: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 })
|
@Column({ comment: '状态 0-禁用 1-启用', default: 0 })
|
||||||
status: number;
|
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 })
|
@Column({ comment: '插件的plugin.json', type: 'json', nullable: true })
|
||||||
pluginJson: any;
|
pluginJson: any;
|
||||||
|
|
||||||
|
|||||||
@ -147,7 +147,11 @@ export class PluginCenterService {
|
|||||||
}
|
}
|
||||||
const plugins = await this.pluginInfoEntity.findBy(find);
|
const plugins = await this.pluginInfoEntity.findBy(find);
|
||||||
for (const plugin of plugins) {
|
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 = {
|
const pluginInfo = {
|
||||||
...plugin.pluginJson,
|
...plugin.pluginJson,
|
||||||
config: this.getConfig(plugin.config),
|
config: this.getConfig(plugin.config),
|
||||||
|
|||||||
@ -9,9 +9,11 @@ import { Equal, In, Not, Repository } from 'typeorm';
|
|||||||
import { PluginInfoEntity } from '../entity/info';
|
import { PluginInfoEntity } from '../entity/info';
|
||||||
import {
|
import {
|
||||||
Config,
|
Config,
|
||||||
|
ILogger,
|
||||||
IMidwayApplication,
|
IMidwayApplication,
|
||||||
IMidwayContext,
|
IMidwayContext,
|
||||||
InjectClient,
|
InjectClient,
|
||||||
|
Logger,
|
||||||
} from '@midwayjs/core';
|
} from '@midwayjs/core';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import { PluginInfo } from '../interface';
|
import { PluginInfo } from '../interface';
|
||||||
@ -23,7 +25,8 @@ import {
|
|||||||
} from '../event/init';
|
} from '../event/init';
|
||||||
import { PluginMap, AnyString } from '../../../../typings/plugin';
|
import { PluginMap, AnyString } from '../../../../typings/plugin';
|
||||||
import { PluginTypesService } from './types';
|
import { PluginTypesService } from './types';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as fs from 'fs';
|
||||||
/**
|
/**
|
||||||
* 插件信息
|
* 插件信息
|
||||||
*/
|
*/
|
||||||
@ -53,6 +56,9 @@ export class PluginService extends BaseService {
|
|||||||
@Inject()
|
@Inject()
|
||||||
pluginTypesService: PluginTypesService;
|
pluginTypesService: PluginTypesService;
|
||||||
|
|
||||||
|
@Logger()
|
||||||
|
logger: ILogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增或更新
|
* 新增或更新
|
||||||
* @param param
|
* @param param
|
||||||
@ -106,6 +112,8 @@ export class PluginService extends BaseService {
|
|||||||
const list = await this.pluginInfoEntity.findBy({ id: In(ids) });
|
const list = await this.pluginInfoEntity.findBy({ id: In(ids) });
|
||||||
for (const item of list) {
|
for (const item of list) {
|
||||||
await this.remove(item.keyName, !!item.hook);
|
await this.remove(item.keyName, !!item.hook);
|
||||||
|
// 删除文件
|
||||||
|
await this.deleteData(item.keyName);
|
||||||
}
|
}
|
||||||
await this.pluginInfoEntity.delete(ids);
|
await this.pluginInfoEntity.delete(ids);
|
||||||
}
|
}
|
||||||
@ -318,14 +326,6 @@ export class PluginService extends BaseService {
|
|||||||
hook: pluginJson.hook,
|
hook: pluginJson.hook,
|
||||||
readme,
|
readme,
|
||||||
logo,
|
logo,
|
||||||
content: {
|
|
||||||
type: 'comm',
|
|
||||||
data: content,
|
|
||||||
},
|
|
||||||
tsContent: {
|
|
||||||
type: 'ts',
|
|
||||||
data: tsContent,
|
|
||||||
},
|
|
||||||
description: pluginJson.description,
|
description: pluginJson.description,
|
||||||
pluginJson,
|
pluginJson,
|
||||||
config: pluginJson.config,
|
config: pluginJson.config,
|
||||||
@ -345,8 +345,101 @@ export class PluginService extends BaseService {
|
|||||||
// 全新安装
|
// 全新安装
|
||||||
await this.pluginInfoEntity.insert(data);
|
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);
|
this.pluginTypesService.generateDtsFile(pluginJson.key, tsContent);
|
||||||
// 初始化插件
|
// 初始化插件
|
||||||
await this.reInit(pluginJson.key);
|
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 * as ts from 'typescript';
|
||||||
import { Utils } from '../../../comm/utils';
|
import { Utils } from '../../../comm/utils';
|
||||||
import { PluginInfoEntity } from '../entity/info';
|
import { PluginInfoEntity } from '../entity/info';
|
||||||
|
import { PluginService } from './info';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插件类型服务
|
* 插件类型服务
|
||||||
@ -21,6 +22,9 @@ export class PluginTypesService extends BaseService {
|
|||||||
@InjectEntityModel(PluginInfoEntity)
|
@InjectEntityModel(PluginInfoEntity)
|
||||||
pluginInfoEntity: Repository<PluginInfoEntity>;
|
pluginInfoEntity: Repository<PluginInfoEntity>;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
pluginService: PluginService;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
utils: Utils;
|
utils: Utils;
|
||||||
|
|
||||||
@ -239,7 +243,11 @@ export class PluginTypesService extends BaseService {
|
|||||||
.select(['a.id', 'a.status', 'a.tsContent', 'a.keyName'])
|
.select(['a.id', 'a.status', 'a.tsContent', 'a.keyName'])
|
||||||
.getMany();
|
.getMany();
|
||||||
for (const pluginInfo of pluginInfos) {
|
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) {
|
if (tsContent) {
|
||||||
await this.generateDtsFile(pluginInfo.keyName, tsContent);
|
await this.generateDtsFile(pluginInfo.keyName, tsContent);
|
||||||
await this.utils.sleep(200);
|
await this.utils.sleep(200);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user