兼容打包成二进制

This commit is contained in:
COOL 2025-01-15 20:48:27 +08:00
parent b5b448689f
commit 4d736987f2
4 changed files with 61 additions and 14 deletions

View File

@ -43,3 +43,14 @@ ${exportEntities}
fs.writeFileSync(OUTPUT_FILE, fileContent); fs.writeFileSync(OUTPUT_FILE, fileContent);
console.log('Entities file generated successfully!'); 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!');
}

View File

@ -2,7 +2,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { check } from './check'; import { check } from './check';
import { generateEntitiesFile } from './entity'; import { generateEntitiesFile, clearEntitiesFile } from './entity';
import { obfuscate } from './obfuscate'; import { obfuscate } from './obfuscate';
const program = new Command(); const program = new Command();
@ -12,15 +12,22 @@ program.version(require('../../package.json').version);
// 修改命令定义部分 // 修改命令定义部分
const commands = { const commands = {
check: async () => await check(), 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(), obfuscate: async () => await obfuscate(),
}; };
// 移除原有的单独命令定义 // 移除原有的单独命令定义
program program
.arguments('[cmds...]') .arguments('[cmds...]')
.option('--clear', 'Clear entities file when using entity command')
.description('Run one or multiple commands: check, entity, obfuscate') .description('Run one or multiple commands: check, entity, obfuscate')
.action(async (cmds: string[]) => { .action(async (cmds: string[], options) => {
if (!cmds.length) { if (!cmds.length) {
program.outputHelp(); program.outputHelp();
return; return;
@ -29,7 +36,7 @@ program
for (const cmd of cmds) { for (const cmd of cmds) {
if (cmd in commands) { if (cmd in commands) {
console.log(`Executing ${cmd}...`); console.log(`Executing ${cmd}...`);
await commands[cmd](); await commands[cmd](options);
} else { } else {
console.error(`Unknown command: ${cmd}`); console.error(`Unknown command: ${cmd}`);
} }

View File

@ -2,9 +2,11 @@ import {
App, App,
ILifeCycle, ILifeCycle,
ILogger, ILogger,
IMidwayApplication,
IMidwayContainer, IMidwayContainer,
Inject, Inject,
Logger, Logger,
MidwayWebRouterService,
} from '@midwayjs/core'; } from '@midwayjs/core';
import { Configuration } from '@midwayjs/core'; import { Configuration } from '@midwayjs/core';
import * as DefaultConfig from './config/config.default'; import * as DefaultConfig from './config/config.default';
@ -18,6 +20,7 @@ import { CoolEps } from './rest/eps';
import { CoolDecorator } from './decorator'; import { CoolDecorator } from './decorator';
import * as cache from '@midwayjs/cache-manager'; import * as cache from '@midwayjs/cache-manager';
import * as _cache from '@midwayjs/cache'; import * as _cache from '@midwayjs/cache';
import { LocationUtil } from './util/location';
@Configuration({ @Configuration({
namespace: 'cool', namespace: 'cool',
@ -38,6 +41,9 @@ export class CoolConfiguration implements ILifeCycle {
@Inject() @Inject()
coolEventManager: CoolEventManager; coolEventManager: CoolEventManager;
@Inject()
webRouterService: MidwayWebRouterService;
async onReady(container: IMidwayContainer) { async onReady(container: IMidwayContainer) {
this.coolEventManager.emit('onReady'); this.coolEventManager.emit('onReady');
// 处理模块配置 // 处理模块配置
@ -48,17 +54,25 @@ export class CoolConfiguration implements ILifeCycle {
this.app.useFilter([CoolExceptionFilter]); this.app.useFilter([CoolExceptionFilter]);
// 装饰器 // 装饰器
await container.getAsync(CoolDecorator); await container.getAsync(CoolDecorator);
// 注册一个路由,用于处理静态资源
// 缓存设置为全局 this.webRouterService.addRouter(
// global["COOL-CACHE"] = await container.getAsync(CacheManager); async ctx => {
// // 清除 location ctx.redirect('/public/index.html');
// setTimeout(() => { },
// location.clean(); {
// this.coreLogger.info("\x1B[36m [cool:core] location clean \x1B[0m"); url: '/',
// }, 10000); 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) { async onServerReady(container: IMidwayContainer) {
// 事件 // 事件

View File

@ -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 fs from 'fs';
import * as path from 'path'; import * as path from 'path';
@ -10,6 +10,21 @@ import * as path from 'path';
export class LocationUtil { export class LocationUtil {
private locationCache = new Map<string, any>(); private locationCache = new Map<string, any>();
distPath: string;
@Init()
async init() {
this.distPath = this.getRunPath();
}
/**
*
* @returns
*/
getDistPath(): string {
return this.distPath;
}
/** /**
* *
* @param target * @param target