Skip to content

Commit

Permalink
fix: bootstrap close entry (#3000)
Browse files Browse the repository at this point in the history
* fix: bootstrap close with entry

* fix: bootstrap close with entry

* fix: bootstrap close with entry
  • Loading branch information
czy88840616 committed May 31, 2023
1 parent f7fbe65 commit ce624ed
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
63 changes: 40 additions & 23 deletions packages/mock/src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
} from '@midwayjs/core';
import { isAbsolute, join, resolve } from 'path';
import { clearAllLoggers } from '@midwayjs/logger';
import { ComponentModule, MockAppConfigurationOptions } from './interface';
import {
ComponentModule,
MockAppConfigurationOptions,
IBootstrapAppStarter,
} from './interface';
import {
findFirstExistModule,
isTestEnvironment,
Expand Down Expand Up @@ -213,33 +217,41 @@ export async function createApp<
return framework.getApplication();
}

export async function close<T extends IMidwayApplication<any>>(
app: T,
export async function close(
app: IMidwayApplication<any> | { close: (...args) => void },
options?: {
cleanLogsDir?: boolean;
cleanTempDir?: boolean;
sleep?: number;
}
) {
if (!app) return;
debug(`[mock]: Closing app, appDir=${app.getAppDir()}`);
options = options || {};

await destroyGlobalApplicationContext(app.getApplicationContext());
if (isTestEnvironment()) {
// clean first
if (options.cleanLogsDir && !isWin32()) {
await removeFile(join(app.getAppDir(), 'logs'));
}
if (MidwayFrameworkType.WEB === app.getFrameworkType()) {
if (options.cleanTempDir && !isWin32()) {
await removeFile(join(app.getAppDir(), 'run'));
if (
app instanceof BootstrapAppStarter ||
(typeof app['close'] === 'function' && !app['getConfig'])
) {
await app['close'](options);
} else {
app = app as IMidwayApplication<any>;
debug(`[mock]: Closing app, appDir=${app.getAppDir()}`);
options = options || {};

await destroyGlobalApplicationContext(app.getApplicationContext());
if (isTestEnvironment()) {
// clean first
if (options.cleanLogsDir && !isWin32()) {
await removeFile(join(app.getAppDir(), 'logs'));
}
if (MidwayFrameworkType.WEB === app.getFrameworkType()) {
if (options.cleanTempDir && !isWin32()) {
await removeFile(join(app.getAppDir(), 'run'));
}
}
if (options.sleep > 0) {
await sleep(options.sleep);
} else {
await sleep(50);
}
}
if (options.sleep > 0) {
await sleep(options.sleep);
} else {
await sleep(50);
}
}
}
Expand Down Expand Up @@ -520,7 +532,7 @@ class LightFramework extends BaseFramework<any, any, any, any, any> {
}
}

class BootstrapAppStarter {
class BootstrapAppStarter implements IBootstrapAppStarter {
getApp(type: MidwayFrameworkType | string): IMidwayApplication<any> {
const applicationContext = getCurrentApplicationContext();
const applicationManager = applicationContext.get(MidwayApplicationManager);
Expand Down Expand Up @@ -577,10 +589,15 @@ export async function createLightApp(
export async function createBootstrap(
entryFile: string,
options: MockAppConfigurationOptions = {}
): Promise<BootstrapAppStarter> {
): Promise<IBootstrapAppStarter> {
if (safeRequire('@midwayjs/faas')) {
options.entryFile = entryFile;
return createFunctionApp(process.cwd(), options);
const app = await createFunctionApp(process.cwd(), options);
return {
close: async () => {
return close(app);
},
};
} else {
await create(undefined, {
entryFile,
Expand Down
7 changes: 6 additions & 1 deletion packages/mock/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMidwayBootstrapOptions } from '@midwayjs/core';
import { IMidwayApplication, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';

export interface MockAppConfigurationOptions extends IMidwayBootstrapOptions {
cleanLogsDir?: boolean;
Expand All @@ -10,3 +10,8 @@ export interface MockAppConfigurationOptions extends IMidwayBootstrapOptions {
export type ComponentModule = {
Configuration: new () => any;
};

export interface IBootstrapAppStarter {
getApp?(type: MidwayFrameworkType | string): IMidwayApplication<any>;
close(options?: { sleep?: number }): Promise<void>;
}

0 comments on commit ce624ed

Please sign in to comment.