1.0.2 • Published 3 years ago

mh-config v1.0.2

Weekly downloads
3
License
MIT
Repository
-
Last release
3 years ago

mh-config

mh-config is a zero-dependency module that helps provide configuration to your existing or new applications in no time.

Storing configuration separate from code (not hard-coding) is based on The Twelve-Factor App methodology.

TOC

Introduction

Key features

  • Specify configuration in any file format: ini/json/js
  • Provide these configurations either via a config object or simply process environment variables
  • No need to edit your application's code, if you'd like it that way
  • Hot reload the config file without restarting or reloading your application

Comparison

How mh-config is different (& hopefully better) from the dotenv module?

  • mh-config accepts a number of files formats such as: ini/json/js
    • So you don't need to change the format of your existing config file
    • Using a '.js' file provide eve more flexibility; you can dynamically create configuration values using javascript e.g. modifying time as per day light saving
  • mh-config can be configured to hot reload the config file while your application is still running. This mean no downtime for your app!
    • This also provides the ability to modify the process environment variables available to your application on the fly

Installation

# with npm
npm install mh-config

# or with Yarn
yarn add mh-config

Usage

Two usages are possible: 1. Usage-1: Include it in your application's code 2. Usage-2: Use without modifying your application's code

Usage-1: Include it in your module/code

const config = require('mh-config')({debug:true}); 
// pass in options like debug = true

if (config.error) {
  throw config.error
}

// assuming a file named config.ini exists with username=notadmin, then

// USE either of the following:
process.env['username']
// OR
config['username']

Usage-2 Without modifying your code

node -r mh-config/do <yourApp.js> config_option_debug=true
      # additional config options ^^^^^^^^^^^^^^^^^^^^^^^^
// yourApp.js

// USE either of the following:
process.env['username']
// OR
config['username']

The config file

By default, mh-config looks for a suitable file in the current working directory (cwd), usually the root of the project. A suitable file is one of the following formats:

  • .ini
  • .json
  • .js

Examples:

; config.ini
username=trump
password="2020"
port=1880
// config.json
{
    "username": "trump",
    "password": "2020",
    "port": 1880
}
// config.js
module.exports = {
    "username": "trump",
    "password": "2020",
    "port": 1880
}

Options

mg-config takes in config in two ways: 1. For Usage-1: via an object 2. For Usage-2: via command line arguments

name (for usage-1)name (for usage-2)descriptionaccepted valuesdefault
filenameconfig_option_filenamethe filename (/with path) where config exists. Should be relative to the CWD''
setEnvconfig_option_setEnvwhether to set process's environment variable as welltrue/falsetrue
overwriteExistingEnvconfig_option_overwriteExistingEnvwhether to overwrite an existing environment variabletrue/falsefalse
watchIntervalMsconfig_option_watchIntervalMshot reload any changes to the config file every n milliSeconds5000
debugconfig_option_debugwhether to print debug messages on consoletrue/falsefalse

Rules

  • Each config values is converted into number if possible, unless surrounded by double/single quotes
  • If filename is not provided, mh-config looks for these file names in CWD and stops looking if it finds one:
    • config.ini
    • config.json
    • config.js
  • mh-config will never mess with your existing process.env unless you specifically ask it to by setting 'overwriteExistingEnv = true'
  • Double and Single quotes are useful to mark a value as text e.g. "100" will be treated as a string while 100 as a number. This is only applicable for .ini files and command line arguments with -r option

License

It's an MIT license; feel free to use as you will.