Skip to content

Commit

Permalink
Merge pull request #2294 from micalevisk/refactor/issue-2275
Browse files Browse the repository at this point in the history
perf: cache loaded configuration file
  • Loading branch information
kamilmysliwiec authored Sep 21, 2023
2 parents b33b2af + 6d1caf2 commit 197b30f
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions lib/configuration/nest-configuration.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@ 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 async load(name?: string): Promise<Required<Configuration>> {
const cacheEntryKey = `${this.reader.constructor.name}:${name}`;
const cachedConfig = loadedConfigsCache.get(cacheEntryKey);
if (cachedConfig) {
return cachedConfig;
}

let loadedConfig: Required<Configuration> | undefined;

const content: string | undefined = name
? await this.reader.read(name)
: await this.reader.readAnyOf([
Expand All @@ -16,23 +32,28 @@ export class NestConfigurationLoader implements ConfigurationLoader {
'nest.json',
]);

if (!content) {
return defaultConfiguration;
}
const fileConfig = JSON.parse(content);
if (fileConfig.compilerOptions) {
return {
...defaultConfiguration,
...fileConfig,
compilerOptions: {
...defaultConfiguration.compilerOptions,
...fileConfig.compilerOptions,
},
};
if (content) {
const fileConfig = JSON.parse(content);
if (fileConfig.compilerOptions) {
loadedConfig = {
...defaultConfiguration,
...fileConfig,
compilerOptions: {
...defaultConfiguration.compilerOptions,
...fileConfig.compilerOptions,
},
};
} else {
loadedConfig = {
...defaultConfiguration,
...fileConfig,
};
}
} else {
loadedConfig = defaultConfiguration;
}
return {
...defaultConfiguration,
...fileConfig,
};

loadedConfigsCache.set(cacheEntryKey, loadedConfig!);
return loadedConfig!;
}
}

0 comments on commit 197b30f

Please sign in to comment.