Skip to content

Commit

Permalink
feat: add dump switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
gxkl committed Oct 22, 2024
1 parent ee536e9 commit a1bd456
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions standalone/standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 10 additions & 7 deletions standalone/standalone/src/EggModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Logger } from '@eggjs/tegg';
export interface EggModuleLoaderOptions {
logger: Logger;
baseDir: string;
dump?: boolean;
}

export class EggModuleLoader {
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions standalone/standalone/src/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface RunnerOptions {
name?: string;
innerObjectHandlers?: Record<string, InnerObject[]>;
dependencies?: (string | ModuleDependency)[];
dump?: boolean;
}

export class Runner {
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions standalone/standalone/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

Expand Down

0 comments on commit a1bd456

Please sign in to comment.