Skip to content

Commit

Permalink
feat: add dump switcher (#252)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced a new optional property `dump` in the
`EggModuleLoaderOptions` and `RunnerOptions` interfaces, allowing users
to control module descriptor dumping.
- Enhanced configurability of the `Runner` class with the new `dump`
option.

- **Bug Fixes**
- Improved error handling and logging during the module descriptor
dumping process.

- **Tests**
- Added new test cases to validate the behavior of the `main` function
with the `dump` option in different configurations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
gxkl authored Oct 22, 2024
1 parent ee536e9 commit 80c312f
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 !== 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

0 comments on commit 80c312f

Please sign in to comment.