diff --git a/core/src/bin/entity.ts b/core/src/bin/entity.ts index bf3cd64..65d9f90 100644 --- a/core/src/bin/entity.ts +++ b/core/src/bin/entity.ts @@ -43,3 +43,14 @@ ${exportEntities} fs.writeFileSync(OUTPUT_FILE, fileContent); console.log('Entities file generated successfully!'); } + +/** + * 清空 entities.ts 文件 + */ +export function clearEntitiesFile() { + const emptyContent = `// 自动生成的文件,请勿手动修改 +export const entities = []; +`; + fs.writeFileSync(OUTPUT_FILE, emptyContent); + console.log('Entities file cleared successfully!'); +} diff --git a/core/src/bin/index.ts b/core/src/bin/index.ts index 815cbd3..e4ef76c 100644 --- a/core/src/bin/index.ts +++ b/core/src/bin/index.ts @@ -2,7 +2,7 @@ import { Command } from 'commander'; import { check } from './check'; -import { generateEntitiesFile } from './entity'; +import { generateEntitiesFile, clearEntitiesFile } from './entity'; import { obfuscate } from './obfuscate'; const program = new Command(); @@ -12,15 +12,22 @@ program.version(require('../../package.json').version); // 修改命令定义部分 const commands = { check: async () => await check(), - entity: async () => await generateEntitiesFile(), + entity: async (options: { clear?: boolean } = {}) => { + if (options.clear) { + await clearEntitiesFile(); + } else { + await generateEntitiesFile(); + } + }, obfuscate: async () => await obfuscate(), }; // 移除原有的单独命令定义 program .arguments('[cmds...]') + .option('--clear', 'Clear entities file when using entity command') .description('Run one or multiple commands: check, entity, obfuscate') - .action(async (cmds: string[]) => { + .action(async (cmds: string[], options) => { if (!cmds.length) { program.outputHelp(); return; @@ -29,7 +36,7 @@ program for (const cmd of cmds) { if (cmd in commands) { console.log(`Executing ${cmd}...`); - await commands[cmd](); + await commands[cmd](options); } else { console.error(`Unknown command: ${cmd}`); } diff --git a/core/src/configuration.ts b/core/src/configuration.ts index 86e3957..7ad244e 100644 --- a/core/src/configuration.ts +++ b/core/src/configuration.ts @@ -2,9 +2,11 @@ import { App, ILifeCycle, ILogger, + IMidwayApplication, IMidwayContainer, Inject, Logger, + MidwayWebRouterService, } from '@midwayjs/core'; import { Configuration } from '@midwayjs/core'; import * as DefaultConfig from './config/config.default'; @@ -18,6 +20,7 @@ import { CoolEps } from './rest/eps'; import { CoolDecorator } from './decorator'; import * as cache from '@midwayjs/cache-manager'; import * as _cache from '@midwayjs/cache'; +import { LocationUtil } from './util/location'; @Configuration({ namespace: 'cool', @@ -38,6 +41,9 @@ export class CoolConfiguration implements ILifeCycle { @Inject() coolEventManager: CoolEventManager; + @Inject() + webRouterService: MidwayWebRouterService; + async onReady(container: IMidwayContainer) { this.coolEventManager.emit('onReady'); // 处理模块配置 @@ -48,17 +54,25 @@ export class CoolConfiguration implements ILifeCycle { this.app.useFilter([CoolExceptionFilter]); // 装饰器 await container.getAsync(CoolDecorator); - - // 缓存设置为全局 - // global["COOL-CACHE"] = await container.getAsync(CacheManager); - // // 清除 location - // setTimeout(() => { - // location.clean(); - // this.coreLogger.info("\x1B[36m [cool:core] location clean \x1B[0m"); - // }, 10000); + // 注册一个路由,用于处理静态资源 + this.webRouterService.addRouter( + async ctx => { + ctx.redirect('/public/index.html'); + }, + { + url: '/', + requestMethod: 'GET', + } + ); } - async onConfigLoad() {} + async onConfigLoad(container: IMidwayContainer, app: IMidwayApplication) { + await container.getAsync(LocationUtil); + // 替换app的getBaseDir + app.getBaseDir = () => { + return container.get(LocationUtil).getDistPath(); + }; + } async onServerReady(container: IMidwayContainer) { // 事件 diff --git a/core/src/util/location.ts b/core/src/util/location.ts index 4246bc8..9c4052c 100644 --- a/core/src/util/location.ts +++ b/core/src/util/location.ts @@ -1,4 +1,4 @@ -import { Provide, Scope, ScopeEnum } from '@midwayjs/core'; +import { Init, Provide, Scope, ScopeEnum } from '@midwayjs/core'; import * as fs from 'fs'; import * as path from 'path'; @@ -10,6 +10,21 @@ import * as path from 'path'; export class LocationUtil { private locationCache = new Map(); + distPath: string; + + @Init() + async init() { + this.distPath = this.getRunPath(); + } + + /** + * 获取编译后的文件路径 + * @returns 编译后的文件路径 + */ + getDistPath(): string { + return this.distPath; + } + /** * 获取目标类的定义位置 * @param target 目标类