2.2.0 • Published 9 months ago
@keg-hub/parse-config v2.2.0
Parse-Config
- Helper package for parsing / loading config files with
.ymland.envextensions - Includes simple template system for dynamically setting config values
Install
- With
yarn=>yarn add @keg-hub/parse-config - With
npm=>npm install @keg-hub/parse-config
Usage
- Require the package in your code as needed
// Import the entire package
const PConf = require('@keg-hub/parse-config')
const ymlObj = await PConf.yml.load(...)
const envObj = await PConf.env.load(...)
const templateStr = await PConf.template.fill(...)
// Import just sections from the package
const { yml, env, template } = require('@keg-hub/parse-config')
const ymlObj = await yml.load(...)
const envObj = await env.load(...)
const templateStr = await template.fill(...)
// Import just specific methods from the package
const { loadYml, loadEnv, fillTemplate } = require('@keg-hub/parse-config')
const ymlObj = await loadYml(...)
const envObj = await loadEnv(...)
const templateStr = await fillTemplate(...)
// Load configs based on environment from both .env and .yaml files
const { loadConfigs } = require('@keg-hub/parse-config')
const configObj = await loadConfigs(...)Loading Config Files
- The
loadConfigsmethod can be used to automatically load config files - The config file names are generated from the passed in options
- All found files are loaded, and merged together as a single config object
Priority
- Priority is based on the name and location of the loaded file
- The
.env, thendefaults.envfiles are always loaded first whennoEnv===false, regardless of their location - Files loaded last, override previously loaded config values
- For
valuesfiles, bothymlandyamlextentions are searched for - Because
yamlis technically the correct extension, it will override theymlcounterpart File Name
- The
- Given the following config options
env===stagingymlName===valuesname===appnoEnv===falsenoYml===falseymlPath===env
- Files will be loaded in the following order,
- First
.ymlfiles, then.yamlfiles, and finally.envfiles. See below example.envthendefaults.env==> Always comes first whennoEnv===falsevalues.ymlthenvalues.yamlstaging.ymlthenstaging.yamlapp.ymlthenapp.yamlvalues_staging.ymlthenvalues_staging.yamlvalues_app.ymlthenvalues_app.yamlvalues_staging_app.ymlthenvalues_staging_app.yamlvalues.staging.ymlthenvalues.staging.yamlvalues.app.ymlthenvalues.app.yamlvalues.staging.app.ymlthenvalues.staging.app.yamlvalues-staging.ymlthenvalues-staging.yamlvalues-app.ymlthenvalues-app.yamlvalues-staging-app.ymlthenvalues-staging-app.yaml.env.staging.env.app.env_app_staging.env.app.staging.env.app-stagingstaging.envapp.envapp_staging.envapp.staging.envapp-staging.env
Locations
- After the file names are generated, they are appended to the generated locations
- Some locations are predefined, but custom locations can also be passed via the
locationsarray config option- Custom locations are ALWAYS checked after the default locations
- This allows for any files loaded from the custom locations to override the defaults
loadConfigs - Config Options
const { loadConfigs } = require('@keg-hub/parse-config')
const configObj = await loadConfigs({
// Environment prefix of the config files to be loaded ( i.e. `production.env` )
env: `local`,
// App prefix of the config files to be loaded ( i.e. `my-app.production.env` )
name: ``,
// Should errors be thrown when a file exists but can't be loaded
error: true
// Skip loading .env files
noEnv: false,
// Skip loading .yaml files
noYml: false,
// The reference name of the values files to be loaded ( i.e. `values.local.yml` )
ymlName: `values`,
// Object path to return from loaded yml files
ymlPath: `env`,
// Format type the config should be returned as ( object | string )
format: `object`,
// Run template replace on the loaded config files
fill: true,
// Template data to fill config file with ( Requires the `fill` options value is `true` )
data: {...},
// RegEx patten when running template replace
pattern: /{{([^}]*)}}/g
// Extra file path and directories to search for config files
locations: [...]
// How found configs should be merged. Must be one of overwrite | join | unique.
mergeStrategy: `overwrite`
})