This repository was archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkhala-factory.ts
53 lines (47 loc) · 1.59 KB
/
khala-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { KhalaApplication } from './khala-application';
import { NestFactory, NestContainer, ApplicationConfig } from '@nestjs/core';
import { InstanceLoader } from '@nestjs/core/injector/instance-loader';
import { DependenciesScanner } from '@nestjs/core/scanner';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { ExceptionsZone } from '@nestjs/core/errors/exceptions-zone';
import { Logger } from '@nestjs/common';
interface KhalaApplicationOptions {
logger: boolean;
}
export class KhalaFactoryStatic {
public async create(
module: any,
options: KhalaApplicationOptions = { logger: false },
): Promise<KhalaApplication> {
const config = new ApplicationConfig();
const container = new NestContainer();
if (options.logger) {
Logger.overrideLogger(options.logger);
}
await this.initialize(module, container, config);
const instance = new KhalaApplication(container, config, options);
return instance;
}
private async initialize(
module: any,
container: NestContainer,
config = new ApplicationConfig(),
) {
const instanceLoader = new InstanceLoader(container);
const dependenciesScanner = new DependenciesScanner(
container,
new MetadataScanner(),
config,
);
try {
await ExceptionsZone.asyncRun(async () => {
await dependenciesScanner.scan(module);
await instanceLoader.createInstancesOfDependencies();
dependenciesScanner.applyApplicationProviders();
});
} catch (e) {
process.abort();
}
}
}
export const KhalaFactory = new KhalaFactoryStatic();