Home Reference Source Test Repository
public class | source

ConfigResolver

Provides the default common config resolution process resolving any extensions and setting default values.

Both file and NPM modules are supported for config extension via the extends as a string or array of strings.

Validation is also available when setting pre and post validation data via setResolverData.

ConfigResolver is modeled after the extensions functionality of ESLint. Please see the respective licenses of the code modified at the end of this file.

Test:

Constructor Summary

Public Constructor
public

Instantiate ConfigResolver potentially destructuring an object passed in with the following keys.

Member Summary

Private Members
private

Accessor entry to default value.

private

_eventbus: EventProxy

private

Accessor entry to typhonjs-object-util validation entry.

private

Accessor entry to typhonjs-object-util validation entry.

private

A list of strings indicating keys which will be updated to an array and merged.

Method Summary

Public Methods
public

Returns the resolver data as a ConfigResolverData object.

public

onPluginLoad(ev: PluginEvent)

Stores any associated plugin eventbus and attempts loading of .

public

postValidate(config: object, configName: string)

Validates a config object for any missing or incorrect parameters after resolving.

public

preValidate(config: object, configName: string)

Validates a config object for any missing or incorrect parameters before and during resolving extended config data.

public

resolve(config: object): *

Resolves any config extension and sets missing default config values.

public

Sets default config values.

public

setResolverData(objectPattern: {"defaultValues": *, "preValidate": *, "postValidate": *, "upgradeMergeList": *})

Sets the config resolver data.

Private Methods
private

_applyExtends(config: Object, filePath: string, relativeTo: string, validate: function, loadedConfigs: string[]): Object

Applies values from the "extends" field in a configuration file.

private

_deepMerge(target: Object, src: Object, combine: boolean, parentKey: string): Object

Merges two config objects.

private

_isFilePath(filePath: string): boolean

Determines if a given string represents a filepath or not using the same conventions as require(), meaning that the first character must be non-alphanumeric and not the @ sign which is used for scoped packages to be considered a file path.

private

_load(filePath: string, relativeTo: string, validate: function, loadedConfigs: string[]): Object

Loads a configuration file from the given file path.

private

_resolveExtends(config: object): *

Provides config extension implementation.

Public Constructors

public constructor(resolverData: ConfigResolverData) source

Instantiate ConfigResolver potentially destructuring an object passed in with the following keys.

Params:

NameTypeAttributeDescription
resolverData ConfigResolverData
  • optional

Any default values to apply after resolution.

Private Members

private _defaultValues: object source

Accessor entry to default value.

private _eventbus: EventProxy source

private _postValidate: object source

Accessor entry to typhonjs-object-util validation entry.

private _preValidate: object source

Accessor entry to typhonjs-object-util validation entry.

private _upgradeMergeList: Array<string> source

A list of strings indicating keys which will be updated to an array and merged.

Public Methods

public getResolverData(): ConfigResolverData source

Returns the resolver data as a ConfigResolverData object.

Note: that this is the active data and a copy is not made.

public onPluginLoad(ev: PluginEvent) source

Stores any associated plugin eventbus and attempts loading of . The following event bindings are available:

config:resolver:resolve: Invokes resolve. config:resolver:validate:pre: Invokes preValidate. config:resolver:validate:post: Invokes postValidate.

Params:

NameTypeAttributeDescription
ev PluginEvent

The plugin event.

public postValidate(config: object, configName: string) source

Validates a config object for any missing or incorrect parameters after resolving.

Params:

NameTypeAttributeDescription
config object

A config object to validate.

configName string
  • optional
  • default: 'config'

Optional name of the config object.

public preValidate(config: object, configName: string) source

Validates a config object for any missing or incorrect parameters before and during resolving extended config data.

Params:

NameTypeAttributeDescription
config object

A config object to validate.

configName string
  • optional
  • default: 'config'

Optional name of the config object.

public resolve(config: object): * source

Resolves any config extension and sets missing default config values.

Params:

NameTypeAttributeDescription
config object

A config object to resolve.

Return:

*

public setDefaultValues(config: object) source

Sets default config values.

Params:

NameTypeAttributeDescription
config object

A config object to set default values that do not already exist.

public setResolverData(objectPattern: {"defaultValues": *, "preValidate": *, "postValidate": *, "upgradeMergeList": *}) source

Sets the config resolver data.

Note: For values of ConfigResolverData not set empty defaults are provided.

Params:

NameTypeAttributeDescription
objectPattern {"defaultValues": *, "preValidate": *, "postValidate": *, "upgradeMergeList": *}
  • optional
  • default: {}

Private Methods

private _applyExtends(config: Object, filePath: string, relativeTo: string, validate: function, loadedConfigs: string[]): Object source

Applies values from the "extends" field in a configuration file.

Params:

NameTypeAttributeDescription
config Object

The configuration information.

filePath string

The file path from which the configuration information was loaded.

relativeTo string
  • optional

The path to resolve relative to.

validate function
  • optional

A function performing validation.

loadedConfigs string[]
  • optional

The config files already loaded.

Return:

Object

A new configuration object with all of the "extends" fields loaded and merged.

private _deepMerge(target: Object, src: Object, combine: boolean, parentKey: string): Object source

Merges two config objects. This will not only add missing keys, but will also modify values to match.

If an object key is included in this._upgradeMergeList it will be upgraded and merged into an array without duplicating elements.

Params:

NameTypeAttributeDescription
target Object

Config object.

src Object

Config object. Overrides in this config object will take priority over the base.

combine boolean
  • optional

Whether to combine arrays or not.

parentKey string
  • optional

The parent key of the merged items if any.

Return:

Object

merged config object.

private _isFilePath(filePath: string): boolean source

Determines if a given string represents a filepath or not using the same conventions as require(), meaning that the first character must be non-alphanumeric and not the @ sign which is used for scoped packages to be considered a file path.

Params:

NameTypeAttributeDescription
filePath string

The string to check.

Return:

boolean

True if it's a filepath, false if not.

private _load(filePath: string, relativeTo: string, validate: function, loadedConfigs: string[]): Object source

Loads a configuration file from the given file path.

Params:

NameTypeAttributeDescription
filePath string

The filename or package name to load the configuration information from.

relativeTo string
  • optional

The path to resolve relative to.

validate function
  • optional

A function performing validation.

loadedConfigs string[]
  • optional

The config files already loaded.

Return:

Object

The configuration information.

private _resolveExtends(config: object): * source

Provides config extension implementation.

Params:

NameTypeAttributeDescription
config object

A config object to resolve.

Return:

*