-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
ConfigurationLoader.ts
89 lines (68 loc) · 2.5 KB
/
ConfigurationLoader.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { pathExists } from 'fs-extra';
import path from 'path';
import { IDatabaseDriver } from '../drivers';
import { Configuration } from './Configuration';
import { Utils } from './Utils';
import { Dictionary } from '../typings';
export class ConfigurationLoader {
static async getConfiguration<D extends IDatabaseDriver = IDatabaseDriver>(validate = true, options: Partial<Configuration> = {}): Promise<Configuration<D>> {
const paths = await ConfigurationLoader.getConfigPaths();
for (let path of paths) {
path = Utils.absolutePath(path);
path = Utils.normalizePath(path);
if (await pathExists(path)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config = require(path);
return new Configuration({ ...(config.default || config), ...options }, validate);
}
}
throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
}
static async getPackageConfig(): Promise<Dictionary> {
if (await pathExists(process.cwd() + '/package.json')) {
return require(process.cwd() + '/package.json');
}
return {};
}
static async getSettings(): Promise<Settings> {
const config = await ConfigurationLoader.getPackageConfig();
return config['mikro-orm'] || {};
}
static async getConfigPaths(): Promise<string[]> {
const paths: string[] = [];
const settings = await ConfigurationLoader.getSettings();
if (process.env.MIKRO_ORM_CLI) {
paths.push(process.env.MIKRO_ORM_CLI);
}
paths.push(...(settings.configPaths || []));
if (settings.useTsNode) {
paths.push('./mikro-orm.config.ts');
}
paths.push('./mikro-orm.config.js');
return paths;
}
static async registerTsNode(configPath = 'tsconfig.json') {
const tsConfigPath = path.join(process.cwd(), configPath);
Utils.requireFrom('ts-node', tsConfigPath).register({
project: tsConfigPath,
transpileOnly: true,
});
if (await pathExists(tsConfigPath)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsConfig = require(tsConfigPath);
/* istanbul ignore next */
const paths = tsConfig.compilerOptions?.paths;
if (paths) {
Utils.requireFrom('tsconfig-paths', tsConfigPath).register({
baseUrl: tsConfig.compilerOptions.baseUrl,
paths: tsConfig.compilerOptions.paths,
});
}
}
}
}
export interface Settings {
useTsNode?: boolean;
tsConfigPath?: string;
configPaths?: string[];
}