From b5b448689f501ecd22b82333c46ac1d14ff94687 Mon Sep 17 00:00:00 2001 From: COOL Date: Tue, 14 Jan 2025 19:46:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=BA=8C=E8=BF=9B=E5=88=B6?= =?UTF-8?q?=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/index.ts | 1 + core/src/module/config.ts | 3 +-- core/src/module/import.ts | 4 ++-- core/src/module/menu.ts | 4 ++-- core/src/util/location.ts | 7 +++++-- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core/src/index.ts b/core/src/index.ts index 6003ac3..e8b4d80 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -50,3 +50,4 @@ export * from './module/menu'; export * from './interface'; export * from './util/func'; export * from './constant/global'; +export * from './util/location'; diff --git a/core/src/module/config.ts b/core/src/module/config.ts index 0e00a9e..18abc21 100644 --- a/core/src/module/config.ts +++ b/core/src/module/config.ts @@ -32,7 +32,7 @@ export class CoolModuleConfig { async init() { const modules = []; // 模块路径 - const moduleBasePath = `${location.getRootPath()}/modules/`; + const moduleBasePath = `${location.getRunPath()}/modules/`; if (!fs.existsSync(moduleBasePath)) { return; @@ -49,7 +49,6 @@ export class CoolModuleConfig { const configPath = fs.existsSync(`${modulePath}/config.ts`) ? `${modulePath}/config.ts` : `${modulePath}/config.js`; - console.log('configPath', configPath); if (fs.existsSync(configPath)) { const moduleConfig: ModuleConfig = require(configPath).default({ app: this.app, diff --git a/core/src/module/import.ts b/core/src/module/import.ts index 9caafeb..e9ec569 100644 --- a/core/src/module/import.ts +++ b/core/src/module/import.ts @@ -112,7 +112,7 @@ export class CoolModuleImport { */ checkFileExist(module: string) { const importLockPath = path.join( - `${location.getRootPath()}`, + `${location.getRunPath()}`, '..', 'lock', 'db' @@ -136,7 +136,7 @@ export class CoolModuleImport { // 计算耗时 const startTime = new Date().getTime(); // 模块路径 - const modulePath = `${location.getRootPath()}/modules/${module}`; + const modulePath = `${location.getRunPath()}/modules/${module}`; // 数据路径 const dataPath = `${modulePath}/db.json`; // 判断文件是否存在 diff --git a/core/src/module/menu.ts b/core/src/module/menu.ts index 1527504..4ebcd81 100644 --- a/core/src/module/menu.ts +++ b/core/src/module/menu.ts @@ -87,7 +87,7 @@ export class CoolModuleMenu { */ async importMenu(module: string, metadatas, lockPath?: string) { // 模块路径 - const modulePath = `${location.getRootPath()}/modules/${module}`; + const modulePath = `${location.getRunPath()}/modules/${module}`; // json 路径 const menuPath = `${modulePath}/menu.json`; // 导入 @@ -139,7 +139,7 @@ export class CoolModuleMenu { */ checkFileExist(module: string) { const importLockPath = path.join( - `${location.getRootPath()}`, + `${location.getRunPath()}`, '..', 'lock', 'menu' diff --git a/core/src/util/location.ts b/core/src/util/location.ts index 9b229a1..4246bc8 100644 --- a/core/src/util/location.ts +++ b/core/src/util/location.ts @@ -1,10 +1,13 @@ +import { Provide, Scope, ScopeEnum } from '@midwayjs/core'; import * as fs from 'fs'; import * as path from 'path'; /** * Location 工具类 */ -class LocationUtil { +@Provide() +@Scope(ScopeEnum.Singleton) +export class LocationUtil { private locationCache = new Map(); /** @@ -56,7 +59,7 @@ class LocationUtil { * 获取使用此包的项目的真实根目录路径 * @returns 项目根目录的绝对路径 */ - getRootPath(): string { + getRunPath(): string { const err = new Error(); const callerfile = err.stack.split('\n')[2].match(/\(([^)]+)\)/)[1]; const dirPath = path.dirname(callerfile);