From a1bd4562e08533bef84b2495167cd6609d5f3d48 Mon Sep 17 00:00:00 2001 From: gxkl Date: Tue, 22 Oct 2024 20:36:06 +0800 Subject: [PATCH 1/2] feat: add dump switcher --- standalone/standalone/package.json | 1 + standalone/standalone/src/EggModuleLoader.ts | 17 +++++++++------- standalone/standalone/src/Runner.ts | 2 ++ standalone/standalone/test/index.test.ts | 21 ++++++++++++++++++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/standalone/standalone/package.json b/standalone/standalone/package.json index 3a5b291a..81eb23fc 100644 --- a/standalone/standalone/package.json +++ b/standalone/standalone/package.json @@ -58,6 +58,7 @@ "@types/mocha": "^10.0.1", "@types/node": "^20.2.4", "cross-env": "^7.0.3", + "mm": "^3.2.1", "mocha": "^10.2.0", "ts-node": "^10.9.1", "typescript": "^5.0.4" diff --git a/standalone/standalone/src/EggModuleLoader.ts b/standalone/standalone/src/EggModuleLoader.ts index 3b11342d..11435519 100644 --- a/standalone/standalone/src/EggModuleLoader.ts +++ b/standalone/standalone/src/EggModuleLoader.ts @@ -13,6 +13,7 @@ import { Logger } from '@eggjs/tegg'; export interface EggModuleLoaderOptions { logger: Logger; baseDir: string; + dump?: boolean; } export class EggModuleLoader { @@ -26,13 +27,15 @@ export class EggModuleLoader { private static generateAppGraph(moduleReferences: readonly ModuleReference[], options: EggModuleLoaderOptions) { const moduleDescriptors = LoaderFactory.loadApp(moduleReferences); - for (const moduleDescriptor of moduleDescriptors) { - ModuleDescriptorDumper.dump(moduleDescriptor, { - dumpDir: options.baseDir, - }).catch(e => { - e.message = 'dump module descriptor failed: ' + e.message; - options.logger.warn(e); - }); + if (options.dump) { + for (const moduleDescriptor of moduleDescriptors) { + ModuleDescriptorDumper.dump(moduleDescriptor, { + dumpDir: options.baseDir, + }).catch(e => { + e.message = 'dump module descriptor failed: ' + e.message; + options.logger.warn(e); + }); + } } const globalGraph = GlobalGraph.create(moduleDescriptors); return globalGraph; diff --git a/standalone/standalone/src/Runner.ts b/standalone/standalone/src/Runner.ts index 9bddde88..7f836007 100644 --- a/standalone/standalone/src/Runner.ts +++ b/standalone/standalone/src/Runner.ts @@ -60,6 +60,7 @@ export interface RunnerOptions { name?: string; innerObjectHandlers?: Record; dependencies?: (string | ModuleDependency)[]; + dump?: boolean; } export class Runner { @@ -147,6 +148,7 @@ export class Runner { this.loadUnitLoader = new EggModuleLoader(this.moduleReferences, { logger: ((this.innerObjects.logger && this.innerObjects.logger[0])?.obj as Logger) || console, baseDir: this.cwd, + dump: options?.dump ?? true, }); GlobalGraph.instance!.registerBuildHook(crossCutGraphHook); GlobalGraph.instance!.registerBuildHook(pointCutGraphHook); diff --git a/standalone/standalone/test/index.test.ts b/standalone/standalone/test/index.test.ts index eff49a5e..cd942d4b 100644 --- a/standalone/standalone/test/index.test.ts +++ b/standalone/standalone/test/index.test.ts @@ -1,16 +1,33 @@ import { strict as assert, deepStrictEqual } from 'node:assert'; import path from 'node:path'; import fs from 'node:fs/promises'; -import { ModuleConfig, ModuleConfigs } from '@eggjs/tegg/helper'; +import { setTimeout as sleep } from 'node:timers/promises'; +import mm from 'mm'; +import { ModuleConfig, ModuleConfigs, ModuleDescriptorDumper } from '@eggjs/tegg/helper'; import { main, StandaloneContext, Runner, preLoad } from '..'; import { crosscutAdviceParams, pointcutAdviceParams } from './fixtures/aop-module/Hello'; import { Foo } from './fixtures/dal-module/src/Foo'; describe('standalone/standalone/test/index.test.ts', () => { describe('simple runner', () => { + const fixture = path.join(__dirname, './fixtures/simple'); + + beforeEach(() => { + mm.restore(); + mm.spy(ModuleDescriptorDumper, 'dump'); + }); + it('should work', async () => { - const msg: string = await main(path.join(__dirname, './fixtures/simple')); + const msg: string = await main(fixture); assert(msg === 'hello!hello from ctx'); + await sleep(500); + assert.equal((ModuleDescriptorDumper.dump as any).called, 1); + }); + + it('should not dump', async () => { + await main(fixture, { dump: false }); + await sleep(500); + assert.equal((ModuleDescriptorDumper.dump as any).called, undefined); }); }); From 5fc779b998b8521a0598357cdd2920a8038aef4d Mon Sep 17 00:00:00 2001 From: gxkl Date: Tue, 22 Oct 2024 20:51:49 +0800 Subject: [PATCH 2/2] f --- standalone/standalone/src/EggModuleLoader.ts | 2 +- standalone/standalone/src/Runner.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/standalone/standalone/src/EggModuleLoader.ts b/standalone/standalone/src/EggModuleLoader.ts index 11435519..b0befaad 100644 --- a/standalone/standalone/src/EggModuleLoader.ts +++ b/standalone/standalone/src/EggModuleLoader.ts @@ -27,7 +27,7 @@ export class EggModuleLoader { private static generateAppGraph(moduleReferences: readonly ModuleReference[], options: EggModuleLoaderOptions) { const moduleDescriptors = LoaderFactory.loadApp(moduleReferences); - if (options.dump) { + if (options.dump !== false) { for (const moduleDescriptor of moduleDescriptors) { ModuleDescriptorDumper.dump(moduleDescriptor, { dumpDir: options.baseDir, diff --git a/standalone/standalone/src/Runner.ts b/standalone/standalone/src/Runner.ts index 7f836007..358c9428 100644 --- a/standalone/standalone/src/Runner.ts +++ b/standalone/standalone/src/Runner.ts @@ -148,7 +148,7 @@ export class Runner { this.loadUnitLoader = new EggModuleLoader(this.moduleReferences, { logger: ((this.innerObjects.logger && this.innerObjects.logger[0])?.obj as Logger) || console, baseDir: this.cwd, - dump: options?.dump ?? true, + dump: options?.dump, }); GlobalGraph.instance!.registerBuildHook(crossCutGraphHook); GlobalGraph.instance!.registerBuildHook(pointCutGraphHook);