-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathboot-file.loader.ts
83 lines (75 loc) · 2.42 KB
/
boot-file.loader.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
import { defaultsDeep } from 'lodash';
import { Inject, Injectable } from '@nestjs/common';
import * as path from 'path';
import * as fs from 'fs';
import * as YAML from 'yamljs';
import { BOOT_OPTIONS_PROVIDER } from './boot.constants';
import { BootOptions } from './interfaces/boot-options.interface';
@Injectable()
export class BootFileLoader {
private readonly files: string[];
constructor(
@Inject(BOOT_OPTIONS_PROVIDER) private readonly options: BootOptions,
) {
this.files = this.getFilesPath();
}
public load(): any {
const configs = [];
this.checkFileExists();
this.files.forEach((file, index) => {
configs.push(this.loadFile(file));
});
return defaultsDeep({}, ...configs);
}
private checkFileExists() {
if (this.files.length === 0) {
throw new Error(`file ${path} was not found`);
}
let existFiles: number = 0;
for (let i = 0; i < this.files.length; i++) {
if (fs.existsSync(this.files[i])) {
existFiles++;
}
}
if (existFiles === 0) {
throw new Error(`file ${path} was not found`);
}
}
public loadFile(path: string): any {
let config = {};
if (!fs.existsSync(path)) {
return config;
}
const configStr = fs.readFileSync(path).toString();
try {
config = YAML.parse(configStr);
} catch (e) {
try {
config = JSON.parse(configStr);
} catch (e) {
throw new Error(`file ${path} parse error`);
}
}
return config;
}
private getFilesPath(): string[] {
const filenames: string[] = [];
const env = process.env.NODE_ENV || 'development';
const dirname = path.dirname(this.options.filePath);
const filename = path.basename(this.options.filePath);
const tokens = /(.+)\.(.+)/.exec(filename);
if (tokens) {
tokens.reverse().pop();
filenames.push(
path.resolve(dirname, `${tokens[1]}.${tokens[0]}`),
path.resolve(dirname, `${tokens[1]}.${env}.${tokens[0]}`),
);
} else {
filenames.push(
path.resolve(dirname, filename),
path.resolve(dirname, `${filename}.${env}`),
);
}
return filenames;
}
}