art-config
v2.0.5
Published
A powerful yet simple tool for configuring all your libraries consistently.
Downloads
621
Readme
Art-Config
A powerful yet simple tool for configuring all your libraries consistently in one place under one configuration hierarchy.
Code Basics
- Declare a
Configurable
wherever you need config values. - Declare a
Configuration
for Development, Production and/or test - and run
configure()
when initializing your app.
Configuration Sources
ArtConfig supports reading the artConfigName
and artConfig
configuration values from many sources:
- environment:
- NodeJS: system variables (e.g.
export artConfigName=dev
) - Browser: query params (e.g.
?artConfigName=dev
)
- NodeJS: system variables (e.g.
- options passed at runtime (e.g.
&ArtConfig.configure(artConfigName: dev)
) - globals (e.g.
window.artConfigName = dev;
) Configuration
selected by artConfigName- Each
Configurable
's defaults
Install
npm install art-config
Usage
Concepts
ArtConfig is a hierarchical configuration system. It provides a universal way to configure all libraries and applications in one hierarchical namespace. ArtConfig standardizes how configuration values are collected, normalized and registered with each registered Configurable.
Concept: The ArtConfig Tree
All config managed by ArtConfig is represented in a hierarchy of plain javascript objects. You can access this entire tree by calling: &ArtConfig.getArtConfig()
The paths in this hierarchy are defined by the Neptune Namespaces in which each Configurable is declared.
Example:
import &ArtConfig
# This configurable
# source/Any/Path/You/Want/Config.caf
class Config extends Configurable
@defaults
foo: :bar
# is accessible at this path:
getArtConfig().Any.Path.You.Want
# Therefor the following asserts will pass:
# (assuming there is only the one Configurable registered)
# (assert.eq is from ArtTestbench and uses deep-equality)
assert.eq
getArtConfig().Any.Path.You.Want.foo
:bar
assert.eq
getArtConfig()
Any: Path: You: Want: foo: :bar
Concept: Configurable (class to inherit from)
Declare slices of the artConfig tree to be configured. Each Configurable can have default values and post-configure hooks.
# source/MyApp/Config.caf
import &ArtConfig
class Config extends Configurable
@defaults
serverUrl: "https://abc.def"
apiKey: null
apiSecret: null
@on configured: ({serverUrl}) ->
openConnection serverUrl
You can access the config values from elsewhere:
# AnotherFile.caf
import &Config
logInfo = -> console.log config.serverUrl
Note: Configurables depend on NeptuneNamespaces to define their configuration-path. The configuration-path is set to the namespaces hierarchy in which your configurable class is declared. You can call
YourConfig.getConfigurationPath()
to determine what your config's path is.
Concept: Configuration (class to inherit from)
Configurations are automatically registered. They are selected at configure time via the artConfigName
value. There can only be one Configuration for each artConfigName value (e.g. Production, Development, Test, etc...).
class Development extends &ArtConfig.Configuration
MyApp: # Note, this is the configurable's Configuration-Path, see above
serverUrl: "http://localhost:3000"
Concept: configure
during App Initialization
When your application actually starts, you need to run configure to gather up all the configuration data and route them to the correct Configurables.
&ArtConfig.configure()
Loading Configuration
When using ArtConfig, you need to call configure
at some point during your application initialization sequence. This will collect configuration from all the configuration sources (e.g. the environment, the global, the URL params, and any config you pass in explicitly). Configure also selects which config to use.
# most the time you only need this:
&ArtConfig.configure()
# but sometimes you might want to provide some options (e.g. for various Tests)
&ArtConfig.configure
artConfigName: :Test
artConfig: MyApp: serverUrl: "test.bad.url"
Inputs
The input is a single, options object with these optional fields:
artConfigName: string
can be passed in: as an argument` via process.env via the browser query string
default: "Development"
EFFECT: ArtConfig.artConfigName = externalEnvironment.artConfigName || artConfigName
artConfig: JSON string OR plain object structure
can be passed in: as an argument via process.env via the browser query string
default: {}
EFFECT: mergeInto ArtConfig.artConfig, deepMerge ConfigRegistry.configs[artConfigName] global.artConfig artConfig externalEnvironment.artConfig
onConfig: (artConfig) ->
gets called as soon as artConfig completes with the final artConfig
Effects
callback @artConfig for callback in ConfigRegistry.configurables
EXAMPLES:
import &ArtConfig
# default artConfigName is Development
&ArtConfig.configure
artConfig: {}
&ArtConfig.configure
artConfigName: :Production
artConfig: verbose: true
&ArtConfig.configure
artConfigName: :Test
artConfig: {}
Config Sources
Actual Config data can come from several different places:
- Environment:
- NodeJS: system environment
- Browser: query params
- Options: options passed directly into the configure function
- Configurations: Production, Development, Test or other configuration classes declared in your application.
- Global: global runtime environment (
window
/self
/global
) - Configurable defaults: Configurables have default values
Definition of Terms
environment
:- NodeJS:
process.env
- Browser:
location.search
, parsed (the current URL's query params)
- NodeJS:
options
: the object passed in. e.g.ArtConfig.configure(options)
global
:- NodeJS:
global
- Browser:
window
- NodeJS:
How artConfigName
is Selected
The first non-empty string is used from:
environment.artConfigName
options.artConfigName
global.artConfigName
Note:
artConfigName
are noramlized usingArtStandardLib.upperCamelCase
(e.g.Production
andproduction
are the same). Shortcutsprod
anddev
are allowed and are automatically rewritten toproduction
anddevelopment
.
How artConfig
is Generated
Generating ArtConfig.artConfig
is the essential purpose of ArtConfig. It is a namespaced hierarchy of arbitrary config values. This hierarchy is simply a deep merging of config values from the following sources. This list is in priority order. Higher priority values override lower priority ones (#1 is the highest priority):
ArtConfig Priority:
environment.artConfig
(JSON encoded string)options.artConfig
(plain object data structure)global.artConfig
(plain object data structure)- Configuration (selected from the registered Configurations using artConfigName's value: Production, Development, Test, etc...)
- Configurable defaults