yt-config
v4.0.4
Published
An ini-based advanced config module
Downloads
125
Readme
YT Config
This module has been designed to substitute existing ini-config parsers out there. Some of which lack typecasting, some have too many dependencies, none of them merges configs according to environment.
Main features:
- environment sections (merged with default)
- basic typecasting (values that look like numbers are returned as numbers)
- valid JSON strings are parsed
- no dependencies other than
lodash.merge
Apart from parsing ini format, it achieves two things:
- never repeat config for different environments - you have 'default' section and you override values that differ for other environments
- have all of the config in one file
Example
This ini:
[default]
negInt = -1
posInt = 2
keyDecimal = 10.5
arrayDecimal[] = -10.5
arrayDecimal[] = 12.43
switchOne = on
switchTwo = off
flag = true
falseFlag = false
[default.log]
level = DEBUG
; DEVELOPMENT
[development]
keyDEV = dev
array = ["one", "two", "three", 3]
[development.log]
level = INFO
; STAGING
[staging]
keySTG = stagingValue
[staging.log]
level = ERROR
will result in the following config object:
{ negInt: -1,
posInt: 2,
keyDecimal: 10.5,
arrayDecimal: [ -10.5, 12.43 ],
switchOne: true,
switchTwo: false,
flag: true,
falseFlag: false,
log: { level: 'INFO' },
keyDEV: 'dev',
array: [ 'one', 'two', 'three', 3 ],
environment: 'development' }
(development
is the default environment)
If we set NODE_ENV
to staging
:
{ negInt: -1,
posInt: 2,
keyDecimal: 10.5,
arrayDecimal: [ -10.5, 12.43 ],
switchOne: true,
switchTwo: false,
flag: true,
falseFlag: false,
null: null,
log: { level: 'ERROR' },
keySTG: 'stagingValue',
environment: 'staging' }
For a working example see example.js
Install
npm i yt-config
Usage
const configLoader = require('yt-config');
...
async function someFunc() {
const config = await configLoader('config.ini');
}
Advanced example
If we need to get some values from environment:
[default.db]
port = 3636
host = some.host
user = some_user
password = ENV::DB_PASSWORD
Test
npm test