Skip to content

Commit

Permalink
refactor: export applicationContext for analyze (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurten authored May 25, 2021
1 parent 77c283a commit 10f202e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/util/webRouterCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@midwayjs/decorator';
import { MidwayContainer } from '../context/midwayContainer';
import { joinURLPath } from './index';
import { IMidwayContainer } from '../interface';

export interface RouterInfo {
/**
Expand Down Expand Up @@ -103,6 +104,7 @@ export class WebRouterCollector {
protected routes = new Map<string, RouterInfo[]>();
private routesPriority: RouterPriority[] = [];
protected options: RouterCollectorOptions;
private applicationContext: IMidwayContainer;

constructor(baseDir = '', options: RouterCollectorOptions = {}) {
this.baseDir = baseDir;
Expand All @@ -115,6 +117,10 @@ export class WebRouterCollector {
await framework.initialize({
baseDir: this.baseDir,
});

this.applicationContext = framework.getApplicationContext();
} else {
this.applicationContext = MidwayContainer.parentApplicationContext;
}

const controllerModules = listModule(CONTROLLER_KEY);
Expand Down Expand Up @@ -142,6 +148,10 @@ export class WebRouterCollector {
});
}

public getApplicationContext() {
return this.applicationContext;
}

protected collectRoute(module, functionMeta = false) {
const controllerId = getProviderId(module);
const controllerOption: ControllerOption = getClassMetadata(
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/util/triggerCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('/test/util/triggerCollector.test.ts', function () {
clearContainerCache();
const collector = new ServerlessTriggerCollector(join(__dirname, '../fixtures/base-app-func-router/src'));
const result = await collector.getFunctionList();
expect(collector.getApplicationContext()).not.toBeNull();
expect(collector.getApplicationContext()).not.toBeUndefined();

expect(matchObjectPropertyInArray(result, {
"controllerId": "apiController",
"controllerMiddleware": [
Expand Down

0 comments on commit 10f202e

Please sign in to comment.