-
Notifications
You must be signed in to change notification settings - Fork 378
/
nest-configuration.loader.ts
66 lines (58 loc) 路 2.02 KB
/
nest-configuration.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
import { Reader, ReaderFileLackPersmissionsError } from '../readers';
import { Configuration } from './configuration';
import { ConfigurationLoader } from './configuration.loader';
import { defaultConfiguration } from './defaults';
/**
* A cache table that maps some reader (by its name along with the config path)
* to a loaded configuration.
* This was added because several commands relies on the app's config in order
* to generate some dynanmic content prior running the command itself.
*/
const loadedConfigsCache = new Map<string, Required<Configuration>>();
export class NestConfigurationLoader implements ConfigurationLoader {
constructor(private readonly reader: Reader) {}
public load(name?: string): Required<Configuration> {
const cacheEntryKey = `${this.reader.constructor.name}:${name}`;
const cachedConfig = loadedConfigsCache.get(cacheEntryKey);
if (cachedConfig) {
return cachedConfig;
}
let loadedConfig: Required<Configuration> | undefined;
const contentOrError = name
? this.reader.read(name)
: this.reader.readAnyOf([
'nest-cli.json',
'.nestcli.json',
'.nest-cli.json',
'nest.json',
]);
if (contentOrError) {
const isMissingPersmissionsError =
contentOrError instanceof ReaderFileLackPersmissionsError;
if (isMissingPersmissionsError) {
console.error(contentOrError.message);
process.exit(1);
}
const fileConfig = JSON.parse(contentOrError);
if (fileConfig.compilerOptions) {
loadedConfig = {
...defaultConfiguration,
...fileConfig,
compilerOptions: {
...defaultConfiguration.compilerOptions,
...fileConfig.compilerOptions,
},
};
} else {
loadedConfig = {
...defaultConfiguration,
...fileConfig,
};
}
} else {
loadedConfig = defaultConfiguration;
}
loadedConfigsCache.set(cacheEntryKey, loadedConfig!);
return loadedConfig!;
}
}