优化菜单导入

This commit is contained in:
cool 2024-04-24 09:11:44 +08:00
parent 8deaa90fd4
commit d117b84dd1
3 changed files with 71 additions and 62 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "7.1.17", "version": "7.1.18",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

@ -1,4 +1,14 @@
import { App, Config, ILogger, IMidwayApplication, Inject, Logger, Provide, Scope, ScopeEnum } from "@midwayjs/core"; import {
App,
Config,
ILogger,
IMidwayApplication,
Inject,
Logger,
Provide,
Scope,
ScopeEnum,
} from "@midwayjs/core";
import * as fs from "fs"; import * as fs from "fs";
import { CoolModuleConfig } from "./config"; import { CoolModuleConfig } from "./config";
import * as path from "path"; import * as path from "path";
@ -11,71 +21,70 @@ import { CoolEventManager } from "../event";
@Provide() @Provide()
@Scope(ScopeEnum.Singleton) @Scope(ScopeEnum.Singleton)
export class CoolModuleMenu { export class CoolModuleMenu {
@Inject()
coolModuleConfig: CoolModuleConfig;
@Inject() @Config("cool")
coolModuleConfig: CoolModuleConfig; coolConfig: CoolConfig;
@Config("cool") @App()
coolConfig: CoolConfig; app: IMidwayApplication;
@App() @Logger()
app: IMidwayApplication; coreLogger: ILogger;
@Logger() @Inject()
coreLogger: ILogger; coolEventManager: CoolEventManager;
@Inject() datas = {};
coolEventManager: CoolEventManager;
datas = {}; async init() {
// 是否需要导入
async init() { if (this.coolConfig.initMenu) {
// 是否需要导入 const modules = this.coolModuleConfig.modules;
if (this.coolConfig.initMenu) { const importLockPath = path.join(
const modules = this.coolModuleConfig.modules; `${this.app.getBaseDir()}`,
const importLockPath = path.join( "..",
`${this.app.getBaseDir()}`, "lock",
"..", "menu"
"lock", );
'menu' if (!fs.existsSync(importLockPath)) {
); fs.mkdirSync(importLockPath, { recursive: true });
if (!fs.existsSync(importLockPath)) { }
fs.mkdirSync(importLockPath, { recursive: true }); for (const module of modules) {
} const lockPath = path.join(importLockPath, module + ".menu.lock");
for (const module of modules) { if (!fs.existsSync(lockPath)) {
const lockPath = path.join(importLockPath, module + ".menu.lock"); await this.importMenu(module, lockPath);
if (!fs.existsSync(lockPath)) {
await this.importMenu(module, lockPath);
}
}
this.coolEventManager.emit("onMenuImport", this.datas);
this.coolEventManager.emit("onMenuInit", {});
} }
} }
this.coolEventManager.emit("onMenuImport", this.datas);
/** // this.coolEventManager.emit("onMenuInit", {});
*
* @param module
* @param lockPath
*/
async importMenu(module: string, lockPath: string){
// 模块路径
const modulePath = `${this.app.getBaseDir()}/modules/${module}`;
// json 路径
const menuPath = `${modulePath}/menu.json`;
// 导入
if (fs.existsSync(menuPath)) {
const data = fs.readFileSync(menuPath);
try {
// this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString()));
this.datas[module] = JSON.parse(data.toString());
fs.writeFileSync(lockPath, data);
} catch (error) {
this.coreLogger.error(error);
this.coreLogger.error(
`自动初始化模块[${module}]菜单失败,请检查对应的数据结构是否正确`
);
}
}
} }
} }
/**
*
* @param module
* @param lockPath
*/
async importMenu(module: string, lockPath: string) {
// 模块路径
const modulePath = `${this.app.getBaseDir()}/modules/${module}`;
// json 路径
const menuPath = `${modulePath}/menu.json`;
// 导入
if (fs.existsSync(menuPath)) {
const data = fs.readFileSync(menuPath);
try {
// this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString()));
this.datas[module] = JSON.parse(data.toString());
fs.writeFileSync(lockPath, data);
} catch (error) {
this.coreLogger.error(error);
this.coreLogger.error(
`自动初始化模块[${module}]菜单失败,请检查对应的数据结构是否正确`
);
}
}
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "7.1.17", "version": "7.1.18",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"typings": "index.d.ts", "typings": "index.d.ts",