兼容打包成二进制

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);
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 { 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}`);
}

View File

@ -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) {
// 事件

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