refactor(dev-infra): do not validate config file multiple times (#38808)

Currently we validate the configuration file on each `getConfig`
invocation. We can only validate once since the configuration
is cached.

Also while being at it, renames the cache variables to lower-case as those
do not represent constants (which are convention-wise upper case).

PR Close #38808
This commit is contained in:
Paul Gschwendtner 2020-09-11 17:09:58 +02:00 committed by Andrew Kushnir
parent 354138eba9
commit e162da0753

View File

@ -49,7 +49,7 @@ export type NgDevConfig<T = {}> = CommonConfig&T;
const CONFIG_FILE_PATH = '.ng-dev/config'; const CONFIG_FILE_PATH = '.ng-dev/config';
/** The configuration for ng-dev. */ /** The configuration for ng-dev. */
let CONFIG: {}|null = null; let cachedConfig: NgDevConfig|null = null;
/** /**
* The filename expected for local user config, without the file extension to allow a typescript, * The filename expected for local user config, without the file extension to allow a typescript,
@ -58,7 +58,7 @@ let CONFIG: {}|null = null;
const USER_CONFIG_FILE_PATH = '.ng-dev.user'; const USER_CONFIG_FILE_PATH = '.ng-dev.user';
/** The local user configuration for ng-dev. */ /** The local user configuration for ng-dev. */
let USER_CONFIG: {[key: string]: any}; let userConfig: {[key: string]: any}|null = null;
/** /**
* Get the configuration from the file system, returning the already loaded * Get the configuration from the file system, returning the already loaded
@ -66,15 +66,15 @@ let USER_CONFIG: {[key: string]: any};
*/ */
export function getConfig(): NgDevConfig { export function getConfig(): NgDevConfig {
// If the global config is not defined, load it from the file system. // If the global config is not defined, load it from the file system.
if (CONFIG === null) { if (cachedConfig === null) {
// The full path to the configuration file. // The full path to the configuration file.
const configPath = join(getRepoBaseDir(), CONFIG_FILE_PATH); const configPath = join(getRepoBaseDir(), CONFIG_FILE_PATH);
// Set the global config object. // Read the configuration and validate it before caching it for the future.
CONFIG = readConfigFile(configPath); cachedConfig = validateCommonConfig(readConfigFile(configPath));
} }
// Return a clone of the global config to ensure that a new instance of the config is returned // Return a clone of the cached global config to ensure that a new instance of the config
// each time, preventing unexpected effects of modifications to the config object. // is returned each time, preventing unexpected effects of modifications to the config object.
return validateCommonConfig({...CONFIG}); return {...cachedConfig};
} }
/** Validate the common configuration has been met for the ng-dev command. */ /** Validate the common configuration has been met for the ng-dev command. */
@ -162,13 +162,13 @@ export function getRepoBaseDir() {
*/ */
export function getUserConfig() { export function getUserConfig() {
// If the global config is not defined, load it from the file system. // If the global config is not defined, load it from the file system.
if (USER_CONFIG === undefined) { if (userConfig === null) {
// The full path to the configuration file. // The full path to the configuration file.
const configPath = join(getRepoBaseDir(), USER_CONFIG_FILE_PATH); const configPath = join(getRepoBaseDir(), USER_CONFIG_FILE_PATH);
// Set the global config object. // Set the global config object.
USER_CONFIG = readConfigFile(configPath, true); userConfig = readConfigFile(configPath, true);
} }
// Return a clone of the global config to ensure that a new instance of the config is returned // Return a clone of the user config to ensure that a new instance of the config is returned
// each time, preventing unexpected effects of modifications to the config object. // each time, preventing unexpected effects of modifications to the config object.
return {...USER_CONFIG}; return {...userConfig};
} }