From 487795cf284691aad71188e82ab19e989ccb03ad Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Wed, 3 Mar 2021 19:05:22 +0800 Subject: [PATCH 1/2] fix: empty framework ready --- packages/core/src/util/emptyFramework.ts | 30 ++++++++++ packages/core/src/util/webRouterCollector.ts | 4 +- packages/core/test/baseFramework.test.ts | 62 ++++++++++---------- 3 files changed, 63 insertions(+), 33 deletions(-) diff --git a/packages/core/src/util/emptyFramework.ts b/packages/core/src/util/emptyFramework.ts index a598f2ed0daa..50b3feb650fe 100644 --- a/packages/core/src/util/emptyFramework.ts +++ b/packages/core/src/util/emptyFramework.ts @@ -2,6 +2,9 @@ import { BaseFramework } from '../baseFramework'; import { IMidwayApplication, IMidwayBootstrapOptions } from '../interface'; import { MidwayFrameworkType } from '@midwayjs/decorator'; +/** + * 一个不 ready 的空框架 + */ export class EmptyFramework extends BaseFramework { getApplication(): any { return this.app; @@ -20,4 +23,31 @@ export class EmptyFramework extends BaseFramework { getDefaultContextLoggerClass() { return super.getDefaultContextLoggerClass(); } + + async containerReady() {} + + async afterContainerReady() {} +} + +/** + * 一个全量的空框架 + */ +export class LightFramework extends BaseFramework { + getApplication(): any { + return this.app; + } + + getFrameworkType(): MidwayFrameworkType { + return MidwayFrameworkType.CUSTOM; + } + + async run(): Promise {} + + async applicationInitialize(options: IMidwayBootstrapOptions) { + this.app = {} as IMidwayApplication; + } + + getDefaultContextLoggerClass() { + return super.getDefaultContextLoggerClass(); + } } diff --git a/packages/core/src/util/webRouterCollector.ts b/packages/core/src/util/webRouterCollector.ts index c25506816db3..63e693c6a28e 100644 --- a/packages/core/src/util/webRouterCollector.ts +++ b/packages/core/src/util/webRouterCollector.ts @@ -1,4 +1,4 @@ -import { EmptyFramework } from './emptyFramework'; +import { LightFramework } from './emptyFramework'; import { CONTROLLER_KEY, ControllerOption, @@ -90,7 +90,7 @@ export class WebRouterCollector { protected async analyze() { if (!MidwayContainer.parentDefinitionMetadata) { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: this.baseDir, }); diff --git a/packages/core/test/baseFramework.test.ts b/packages/core/test/baseFramework.test.ts index bc021d44e09e..6cdff76c7479 100644 --- a/packages/core/test/baseFramework.test.ts +++ b/packages/core/test/baseFramework.test.ts @@ -16,7 +16,7 @@ import { import * as mm from 'mm'; import sinon = require('sinon'); import { LifeCycleTest, LifeCycleTest1, TestBinding } from "./fixtures/lifecycle"; -import { EmptyFramework } from '../src/util/emptyFramework'; +import { EmptyFramework, LightFramework } from '../src/util/emptyFramework'; @Provide() class TestModule { @@ -32,7 +32,7 @@ describe('/test/baseFramework.test.ts', () => { }); it.skip('should load js directory and auto disable', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join(__dirname, './fixtures/js-app-loader'), isTsMode: false, @@ -47,7 +47,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should load preload module', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join(__dirname, './fixtures/base-app/src'), preloadModules: [TestModule], @@ -59,7 +59,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should load configuration', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -82,7 +82,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should load config.*.ts by default env', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -98,7 +98,7 @@ describe('/test/baseFramework.test.ts', () => { it('should load config.*.ts by process.env', async () => { mm(process.env, 'NODE_ENV', 'local'); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -118,7 +118,7 @@ describe('/test/baseFramework.test.ts', () => { callback(m); }); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -137,7 +137,7 @@ describe('/test/baseFramework.test.ts', () => { it('should load with no package.json', async () => { mm(process.env, 'MIDWAY_SERVER_ENV', 'local'); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -160,7 +160,7 @@ describe('/test/baseFramework.test.ts', () => { it('should load configuration with namespace', async () => { mm(process.env, 'MIDWAY_SERVER_ENV', 'local'); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -203,7 +203,7 @@ describe('/test/baseFramework.test.ts', () => { it('should load configuration with object', async () => { mm(process.env, 'MIDWAY_SERVER_ENV', 'local'); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -250,7 +250,7 @@ describe('/test/baseFramework.test.ts', () => { // }); it('should load conflict without error', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -268,7 +268,7 @@ describe('/test/baseFramework.test.ts', () => { it('load default env', async () => { mm(process.env, 'NODE_ENV', ''); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -285,7 +285,7 @@ describe('/test/baseFramework.test.ts', () => { it('load prod env', async () => { mm(process.env, 'NODE_ENV', 'prod'); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -302,7 +302,7 @@ describe('/test/baseFramework.test.ts', () => { it('load daily env', async () => { mm(process.env, 'NODE_ENV', 'daily'); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -320,7 +320,7 @@ describe('/test/baseFramework.test.ts', () => { it('load pre env', async () => { mm(process.env, 'NODE_ENV', 'pre'); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -339,7 +339,7 @@ describe('/test/baseFramework.test.ts', () => { it('load local env', async () => { mm(process.env, 'NODE_ENV', 'local'); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -362,7 +362,7 @@ describe('/test/baseFramework.test.ts', () => { it('load default env', async () => { mm(process.env, 'NODE_ENV', ''); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -388,7 +388,7 @@ describe('/test/baseFramework.test.ts', () => { it('load prod env', async () => { mm(process.env, 'NODE_ENV', 'prod'); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -407,7 +407,7 @@ describe('/test/baseFramework.test.ts', () => { it('load daily env', async () => { mm(process.env, 'NODE_ENV', 'daily'); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -426,7 +426,7 @@ describe('/test/baseFramework.test.ts', () => { it('load pre env', async () => { mm(process.env, 'NODE_ENV', 'pre'); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -445,7 +445,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should test aspect decorator', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -469,7 +469,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should inject global value in component', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -484,7 +484,7 @@ describe('/test/baseFramework.test.ts', () => { it('should load component in different type and different env', async () => { mm(process.env, 'NODE_ENV', ''); mm(process.env, 'MIDWAY_SERVER_ENV', ''); - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -500,7 +500,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('lifecycle should be ok', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -544,7 +544,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should get service in a component write with app', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -572,7 +572,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should create logger and match property between framework and app', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); framework.configure({}); await framework.initialize({ baseDir: path.join( @@ -612,7 +612,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should support functional configuration and hook load', async () => { - const framework = new EmptyFramework(); + const framework = new LightFramework(); await framework.initialize({ baseDir: path.join( __dirname, @@ -626,7 +626,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should run multi framework in one process and use cache', async () => { - const framework1 = new EmptyFramework(); + const framework1 = new LightFramework(); framework1.configure({}); await framework1.initialize({ baseDir: path.join( @@ -635,7 +635,7 @@ describe('/test/baseFramework.test.ts', () => { ), }); - class CustomTwoFramework extends EmptyFramework { + class CustomTwoFramework extends LightFramework { async applicationInitialize(options: IMidwayBootstrapOptions) { this.app = {} as IMidwayApplication; } @@ -667,7 +667,7 @@ describe('/test/baseFramework.test.ts', () => { }); it('should run multi framework in one process and container independent', async () => { - const framework1 = new EmptyFramework(); + const framework1 = new LightFramework(); framework1.configure({}); await framework1.initialize({ baseDir: path.join( @@ -676,7 +676,7 @@ describe('/test/baseFramework.test.ts', () => { ), }); - class CustomTwoFramework extends EmptyFramework { + class CustomTwoFramework extends LightFramework { async applicationInitialize(options: IMidwayBootstrapOptions) { this.app = {} as IMidwayApplication; } From 86b140a006c903cb089c7a8d93345f61a1010fc6 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Wed, 3 Mar 2021 19:51:51 +0800 Subject: [PATCH 2/2] fix: lint --- packages/core/test/baseFramework.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/test/baseFramework.test.ts b/packages/core/test/baseFramework.test.ts index 6cdff76c7479..0e91d07b0342 100644 --- a/packages/core/test/baseFramework.test.ts +++ b/packages/core/test/baseFramework.test.ts @@ -16,7 +16,7 @@ import { import * as mm from 'mm'; import sinon = require('sinon'); import { LifeCycleTest, LifeCycleTest1, TestBinding } from "./fixtures/lifecycle"; -import { EmptyFramework, LightFramework } from '../src/util/emptyFramework'; +import { LightFramework } from '../src/util/emptyFramework'; @Provide() class TestModule {