ezconfig
Tool to simplify the configuration of a node application.
Installation
You can get the latest release and the type definitions using npm:
$ npm install ezconfig-node --save
Example
; const defaultConfig = api: port: 8080 specialPort: 7070 otherComponent: array: arrayString: '[]' ; // example onlyprocessenv'PORT' = '9090';processenv'APP_OTHERCOMPONENT_ARRAY' = '[1,2,3,4]';processenv'APP_OTHERCOMPONENT_ARRAYSTRING' = '"[1,2,3,4]"'; const config = ezconfig // file: { "api": { "specialPort": 6060 } } ; console;
Result:
Methods
json
Merge the old configuration with a new json object.
// creates configuration { "a": 2, "b": 3 }ezconfig
jsonFileSync
ezconfig
env
Use the specific environment variable to overwrite a property of the config.
// Creates the object { "api": { port: API_PORT } }ezconfig
Use the second parameter to choose the property.
// Creates the object { "api": { port: PORT_VALUE } }ezconfig
If the property path is not set, it will use the environment variable name in lowercase.
// Creates the object { "api": { port: 8080 }, "port": PORT_VALUE }ezconfig
envAll
Use all the environment variables that start with the specific prefix.
// APP_API_PORT = '8080'// APP_SOMETHING = '"good"' ezconfig.create.envAll'APP'.get // result: { "api": { "port": 8080 }, "something": "good" }
get
Returns the current configuration.
// returns object { "a": 1 }ezconfig.create.get
Caveat
The method env and envAll will keep the properties original names if they already exist with uppercase letters, example:
// SOMETHINGGREAT = "ezconfig again" ezconfig.create.env'SOMETHINGGREAT'.get // result: { "somethingGreat": "ezconfig again" }