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

fix: bootstrap missing create logger #797

Merged
merged 2 commits into from
Jan 10, 2021
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
24 changes: 20 additions & 4 deletions packages/bootstrap/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class BootstrapStarter {
private bootstrapItems: IMidwayFramework<any, any>[] = [];
private globalOptions: Partial<IMidwayBootstrapOptions> = {};

public configure(options: Partial<IMidwayBootstrapOptions>) {
public configure(options: IMidwayBootstrapOptions) {
this.globalOptions = options;
return this;
}
Expand All @@ -27,7 +27,7 @@ export class BootstrapStarter {
}

public async init() {
this.appDir = this.globalOptions.baseDir || process.cwd();
this.appDir = this.globalOptions.appDir || process.cwd();
await Promise.all(
this.getActions('initialize', {
...this.globalOptions,
Expand All @@ -52,6 +52,9 @@ export class BootstrapStarter {
}

private getBaseDir() {
if (this.globalOptions.baseDir) {
return this.globalOptions.baseDir;
}
if (isTypeScriptEnvironment()) {
return join(this.appDir, 'src');
} else {
Expand All @@ -63,18 +66,22 @@ export class BootstrapStarter {
export class Bootstrap {
static starter: BootstrapStarter;
static logger: ILogger;
static configured = false;

/**
* set global configuration for midway
* @param configuration
*/
static configure(configuration: Partial<IMidwayBootstrapOptions>) {
static configure(configuration: IMidwayBootstrapOptions = {}) {
this.configured = true;
if (!this.logger && !configuration.logger) {
this.logger = createConsoleLogger('bootstrapConsole');
if (configuration.logger === false) {
(this.logger as IMidwayLogger)?.disableConsole();
}
configuration.logger = this.logger;
} else {
this.logger = this.logger || configuration.logger as ILogger;
}
this.getStarter().configure(configuration);
return this;
Expand All @@ -97,6 +104,9 @@ export class Bootstrap {
}

static async run() {
if (!this.configured) {
this.configure();
}
// https://nodejs.org/api/process.html#process_signal_events
// https://en.wikipedia.org/wiki/Unix_signal
// kill(2) Ctrl-C
Expand All @@ -121,7 +131,13 @@ export class Bootstrap {
}

static async stop() {
return this.getStarter().stop();
await this.getStarter().stop();
}

static reset() {
this.logger = null;
this.configured = false;
this.starter = null;
}

/**
Expand Down
62 changes: 51 additions & 11 deletions packages/bootstrap/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ import {
IMidwayFramework,
IMidwayApplication,
IMidwayBootstrapOptions,
IMidwayContainer, IConfigurationOptions, MidwayFrameworkType,
IMidwayContainer,
IConfigurationOptions,
MidwayFrameworkType,
} from '@midwayjs/core';
import { clearAllLoggers } from '@midwayjs/logger';
import { join } from 'path';

class TestFrameworkUnit implements IMidwayFramework<any, IConfigurationOptions> {
configurationOptions: IConfigurationOptions;
interface MockConfigurationOptions extends IConfigurationOptions {
port?: number;
}

export const sleep = async (timeout = 1000) => {
return new Promise<void>(resolve => {
setTimeout(resolve, timeout);
});
}

class TestFrameworkUnit implements IMidwayFramework<any, MockConfigurationOptions> {
configurationOptions: MockConfigurationOptions;
bootstrapOptions: IMidwayBootstrapOptions;
options;
app;

configure(options: IConfigurationOptions): TestFrameworkUnit {
configure(options: MockConfigurationOptions): TestFrameworkUnit {
this.options = options;
return this;
}
Expand All @@ -26,6 +40,7 @@ class TestFrameworkUnit implements IMidwayFramework<any, IConfigurationOptions>

async initialize(options: IMidwayBootstrapOptions): Promise<void> {
this.app = {bbb: 22};
this.bootstrapOptions = options;
}

getApplicationContext(): IMidwayContainer {
Expand All @@ -49,11 +64,11 @@ class TestFrameworkUnit implements IMidwayFramework<any, IConfigurationOptions>
}

getAppDir(): string {
return __dirname;
return this.bootstrapOptions.appDir;
}

getBaseDir(): string {
return __dirname;
return this.bootstrapOptions.baseDir;
}

getLogger(): any {
Expand All @@ -71,13 +86,23 @@ class TestFrameworkUnit implements IMidwayFramework<any, IConfigurationOptions>
getProjectName(): string {
return 'test';
}

getFrameworkName() {
return 'midway:mock'
}
}

describe('/test/index.test.ts', () => {
it('create bootstrap case', async () => {

beforeEach(() => {
Bootstrap.reset();
clearAllLoggers();
})

it('create bootstrap case', async () => {
process.env.MIDWAY_TS_MODE = 'true';
const bootstrap = Bootstrap.configure({
baseDir: __dirname,
appDir: __dirname,
});

expect(bootstrap);
Expand All @@ -87,6 +112,8 @@ describe('/test/index.test.ts', () => {
});
await bootstrap.load(framework).run();
expect(framework);
expect(framework.getAppDir()).toEqual(__dirname);
expect(framework.getBaseDir()).toEqual(join(__dirname, 'src'));
expect(framework.getApplicationContext()).toStrictEqual({a: 1});
expect(framework.app).toStrictEqual({bbb: 22});
expect(framework.getApplication()).toStrictEqual({bbb: 22});
Expand All @@ -95,16 +122,29 @@ describe('/test/index.test.ts', () => {
// Bootstrap.configure({})
// .load(new TestFrameworkUnit().configure({port: 7001}))
// .run();
await bootstrap.stop();
process.env.MIDWAY_TS_MODE = '';
// await bootstrap.reset();
});

it('should bootstrap with no console', async () => {
clearAllLoggers();
Bootstrap.logger = null;
const bootstrap = Bootstrap.configure({
baseDir: __dirname,
appDir: __dirname,
logger: false
});
expect(bootstrap);
await bootstrap.run();
await bootstrap.stop();
});

it('should start bootstrap with not configure', async () => {
const framework = new TestFrameworkUnit().configure({
port: 7001,
});
await Bootstrap.load(framework).run();
expect(framework.getAppDir()).toEqual(process.cwd());
// 因为 jest 环境认不出 ts-node
expect(framework.getBaseDir()).toEqual(join(process.cwd(), 'dist'));
await Bootstrap.stop();
});
});
2 changes: 1 addition & 1 deletion packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export interface IMidwayCoreApplication extends IMidwayApplication {}

export interface IMidwayBootstrapOptions {
logger?: ILogger | boolean;
baseDir: string;
baseDir?: string;
appDir?: string;
preloadModules?: any[];
disableAutoLoad?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const removeFileOrDir = async (p) => {
}

export const sleep = async (timeout = 1000) => {
return new Promise(resolve => {
return new Promise<void>(resolve => {
setTimeout(resolve, timeout);
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mock/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function create<

starter
.configure({
baseDir,
appDir: baseDir,
})
.load(framework);

Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const createAppWorkerLoader = () => {
this.bootstrap = new BootstrapStarter();
this.bootstrap
.configure({
baseDir: this.app.appDir,
appDir: this.app.appDir,
})
.load(this.framework);
this.app.beforeStart(async () => {
Expand Down Expand Up @@ -223,7 +223,7 @@ export const createAgentWorkerLoader = () => {
this.bootstrap = new BootstrapStarter();
this.bootstrap
.configure({
baseDir: this.app.appDir,
appDir: this.app.appDir,
})
.load(this.framework);
this.app.beforeStart(async () => {
Expand Down