Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dump switcher #252

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 !== false) {
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,
});
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
Loading