dotconfig 

[Deprecated] App configuration made simple for Node.js. Now moved to 
Supports:
- app configuration file of type
json
or env
(custom type parser can also be implemented through implementing IParser
& using setparser
) Environment
specific configuration overloading- Now with the power of interpolate-json to support interpolation (or parameter substitution) inside app-configuration (strongly recommend to go through the documentation to know the full power of interpolation)
Install
npm install @terran-source/dotconfig
yarn add @terran-source/dotconfig
Usage
Declaration
const { loadConfig } = require('@terran-source/dotconfig');
json
{
"scheme": "http",
"server": "localhost",
"port": "8080",
"baseKey": "baseValue",
"user": {
"name": "${USER_NAME}",
"password": "${USER_PASSWORD}"
},
"url": "${scheme}://${=${user.name}.toLowerCase()=}:${= encodeURIComponent(${user.password}) =}@${server}:${port}"
}
{
"baseKey": "devValue",
"someDevKey": "someDevValue"
}
{
"scheme": "https",
"server": "test.example.com",
"baseKey": "testValue"
}
{
"scheme": "https",
"server": "${APP_SERVER_NAME}",
"port": "${APP_PORT}",
"baseKey": "${BASE_KEY}"
}
const { loadConfig } = require('@terran-source/dotconfig');
let { parsed, error } = loadConfig('app-config.json');
if (error) {
console.log(error);
}
console.log(`parsed: ${JSON.stringify(parsed, null, 2)}`);
console.log(`url: ${process.appConfig.url}`);
USER_NAME=DevUser USER_PASSWORD=P@ssw0rd node index.js
parsed: {
"scheme": "http",
"server": "localhost",
"port": "8080",
"baseKey": "devValue",
"user": {
"name": "DevUser",
"password": "P@ssw0rd"
},
"url": "http://devuser:P%40ssw0rd@localhost:8080",
"someDevKey": "someDevValue"
}
url: http://devuser:P%40ssw0rd@localhost:8080
Let's break it up:
USER_NAME
& USER_PASSWORD
are set through environment variable. This will replace the placeholders ${USER_NAME}
& ${USER_PASSWORD}
in app-config.json. The environment
is not specified. Hence, it'll be set as default value dev
. Now, the loadConfig
function will search for any dev
environment specific configuration (i.e. any file with name app-config.dev
.json in the same directory, where it finds the original app-config.json). If it finds the additional file, it loads the details & overwrite anything, that matches with the base configuration or add anything, that is not present. i.e.
"baseKey": "baseValue"
becomes "baseKey": "devValue"
- additional
"someDevKey": "someDevValue"
key is added
To load environment specific configurations:
let { parsed, error } = loadConfig({ env: 'test', path: 'app-config.json' });
let { parsed, error } = loadConfig('test', { path: 'app-config.json' });
let { parsed, error } = loadConfig(true, { path: 'app-config.json' });
console.log(`parsed: ${JSON.stringify(parsed, null, 2)}`);
console.log(`url: ${process.appConfig.url}`);
NODE_ENV=test USER_NAME=TestUser USER_PASSWORD=P@ssw0rd node index.js
parsed: {
"scheme": "https",
"server": "test.example.com",
"port": "8080",
"baseKey": "testValue",
"user": {
"name": "TestUser",
"password": "P@ssw0rd"
},
"url": "https://testuser:P%40ssw0rd@test.example.com:8080"
}
url: https://testuser:P%40ssw0rd@test.example.com:8080
env
key = "value"
"baseKey"= "baseValue"
'subKey'="subValue"
"someKey"= 'someValue'
devKey=devValue
"baseKey"= "devValue"
'subKey'="subDevValue"
"someKey"= 'someDevValue'
testKey=testValue
"baseKey"= "testValue"
'subKey'="subTestValue"
"someKey"= 'someTestValue'
"baseKey"= "${BASE_KEY}"
'subKey'="${SUB_KEY}"
"someKey"= '${SOME_KEY}'
const { loadConfig } = require('@terran-source/dotconfig');
let { parsed, error } = loadConfig(true, { path: '.env' });
if (error) {
console.log(error);
}
console.log(`parsed: ${JSON.stringify(parsed, null, 2)}`);
console.log(`baseKey: ${process.appConfig.baseKey}`);
For the Production environment, set the proper ENVIRONMENT_VARIABLE
to be interpolated.
NODE_ENV=prod BASE_KEY='some base key' SUB_KEY='some sub key' SOME_KEY='some other key' node index.js
parsed: {
"key": "value",
"baseKey": "some base key",
"subKey": "some sub key",
"someKey": "some other key"
}
baseKey: some base key
Definition
Syntax: loadConfig([true | env | file.extension], options);
The loadConfig
function takes at most 2 parameters
Optional 1st parameter
boolean: true
If the NODE_ENV
environment variable is set as the environment
, then passing true
as first parameter signifies to get env
from NODE_ENV
environment value.
const { loadConfig } = require();
loadConfig(true, { path: 'app-config.json' });
db.connect(process.appConfig.url);
NODE_ENV=test node index.js
NODE_ENV=uat node index.js
NODE_ENV=prod node index.js
environment
- example:
dev
, test
, uat
, staging
, prod
etc.
The application environment name, which to load the configuration file for. Should be lowercase (by tradition) & not contain any space or special character.
filename (with extension)
- example: "custom-config-file.ext"
The configuration file path (either relative or absolute).
const { loadConfig } = require('@terran-source/dotconfig');
loadConfig('app-config.json');
options
{
debug: false,
encoding: 'utf8',
env: 'dev',
path: 'config.json',
type: 'json'
}
more in Configurations
returns:
{
parsed: {},
error: null
}
If it succeeds to load the configuration file & to parse the information containing within, it sets the process.appConfig
with the value & will return with an Object with
parsed
key with the same value as process.appConfig
.error
key, which is null
if succeeds or the error details if fails.
Configurations
The options
setup.
debug
-
type: boolean
-
default: false
Set it true
to turn on logging to help debug why certain things are not working as expected. Can be turned on globally.
encoding
- type:
string
- default:
utf8
The encoding of the configuration file, supplied in path
.
env
In a multi-environment setup (e.g. dev, test, uat, prod), this is the specifier for environment type. In practical case, it seems more logical to not set this one through options
, but to set the NODE_ENV
environment variable and pass true as first parameter to loadConfig
function (see Example).
path
- type:
string
- default:
config.json
The path of the configuration file. It can either be a relative path or an absolute one.
type
- type:
string
- default:
json
- supported types:
json
, env
The configuration file extension.
Functions
const dotconfig = require('@terran-source/dotconfig');
loadConfig()
Described so far since Declaration
& Definition
.
const dotconfig = require('@terran-source/dotconfig');
dotconfig.loadConfig(opt);
const { loadConfig } = require('@terran-source/dotconfig');
loadConfig(opt);
IParser
IParser lets others to implement a custom type of parser (like toml, xml etc.)
const { IParser } = require('@terran-source/dotconfig');
class CustomParseClass extends IParser {
constructor() {
super();
}
parse(filePath, encoding) {
let json =
return json;
}
}
module.exports = new CustomParseClass();
const { loadConfig, setParser } = require('@terran-source/dotconfig');
const customParser = require('/path/to/custom-parser');
setParser('customType', customParser);
loadConfig('custom-app-config.customType');
db.connect(process.appConfig.url);
setParser()
const { loadConfig, setParser } = require('@terran-source/dotconfig');
const customParser = require('/path/to/custom-parser');
setParser('customType', customParser, 'custom-config-file', true);
loadConfig();
loadConfig(true);
db.connect(process.appConfig.url);
debug()
Globally turn on debug
flag.
const dotconfig = require('@terran-source/dotconfig').debug();
dotconfig.debug(false);
reset()
Resets the options.
const dotconfig = require('@terran-source/dotconfig');
let result = dotconfig.loadConfig({
debug: true,
env: 'test',
path: 'app-config.json',
});
let result2 = dotconfig.loadConfig();
interpolation.reset();