Confige
Smart configuration control
Install
npm i confige
Basic Usage
This package by default read the config folder in your working directory:
- config
- app.json
- database.json
- language.json
const config = ;console;console;console;
If folder config does not exist, file config.json will be checked instead.
- config.json
const config = ;console;
You can override the config file path with an environment variable:
// read a folderprocessenvCONFIG_FILE=src/config// read a fileprocessenvCONFIG_FILE=src/configjson// with absolute pathprocessenvCONFIG_FILE=/path/to/src/configjson
If you specified a single file, the config object will be the file itself rather than a nested object with the filename as its key(like folder reading does).
src/config.json
processenvCONFIG_FILE = src / configjson;const config = ;console;
It won't be loaded again once done. To reload configs, you need to call:
// delete cached configconfig__;// reloadconfig = ;
Env File
By default, This package read a .env file in your working directory and get environment variables from this file.
NODE_ENV=productionCONFIG_FILE=src/config.json
You may override this behaviour by passing the following environment variable:
processenvENV_FILE=src/envconst config = ;
You may optionally set ENV_INJECT
to inject env file variables into process.env
.
Existing environment variables won't be overrided.
process.env.ENV_INJECT=trueNODE_ENV=development npm start
# NODE_ENV won't be set because it already existsNODE_ENV=productionCONFIG_FILE=src/config.json
Smart Config File
A bunch of keywords can be used in config files
The $env
object reads environment varaibles with an optional fallback value. With the following env set:
APP_NAME=myappDB_HOST=10.10.10.100DB_USER=admin
The loaded config will be
Here is a list of available keywords:
$env
Get value from environment variables(first try env file, then process.env) with an optional fallback value
- params:
string
|[string, any]
- returns
any
$path
Resolve the path to absolute from current working directory
- params:
string
- returns
string
$file
Resolve the path and read the file with an optional encoding
- params:
string
|[string, string]
- returns
string
|Buffer
$json
Parse the given string into object
- params:
string
- returns
any
$number
Parse the given string into a number
- params:
string
- returns
number
$concat
Concat strings or arrays
- params:
string[]
|[][]
- returns
string
|any[]
$max
Return the max number in the array
- params:
number[]
- returns
number
$min
Return the min number in the array
- params:
number[]
- returns
number
$sum
Sum the numbers in the array
- params:
number[]
- returns
number
$avg
Return the average value of the array
- params:
number[]
- returns
number
$first
Return the first element in the array
- params:
any[]
- returns
any
$last
Return the last element in the array
- params:
any[]
- returns
any
$at
Return element in the array at the given index
- params:
[any[], number]
- returns
any
$asce
Sort the numbers in ascending order
- params:
number[]
- returns
number[]
$asce
Sort the numbers in descending order
- params:
number[]
- returns
number[]
$rand
Return an element at random index
- params:
any[]
- returns
any
$rands
Return the given amount of elements at random indices
- params:
[any[], number]
- returns
any[]
$reverse
Reverse an array
- params:
any[]
- returns
any[]
$slice
Slice an array from the given index to an optional end index
- params:
[any[], number]
|[any[], number, number]
- returns
any[]
$count
Return the length of an array
- params:
any[]
- returns
number
$merge
Return the merge of two objects
- params:
[any, any]
- returns
any
$keys
Return the keys of an object
- params:
any
- returns
string[]
$vals
Return the values of an object
- params:
any
- returns
any[]
$zip
Merge a series of key-value pairs into an object
- params:
[any, any][]
- returns
any
$zap
Split an object into key-value pairs
- params:
any
- returns
[any, any][]
$cond
Return the second or third element based on the boolean value of the first element
- params:
[boolean, any, any]
- returns
any
$and
Return true only if both two elements' boolean values are true
- params:
[boolean, boolean]
- returns
boolean
$or
Return true if any of the two elements' boolean value is true
- params:
[boolean, boolean]
- returns
boolean
$not
Return true only if the given value is false
- params:
boolean
- returns
boolean
$true
Return true if the given value is true or 'true'(case insensitive) or 1 or '1'
- params:
boolean|string
- returns
boolean
$null
Return true if the given value is null or undefined
- params:
any
- returns
boolean
$undefined
Return true only if the given value is undefined
- params:
any
- returns
boolean
$type
Return true only if the given value is of the given type
- params:
[any, string]
- returns
boolean
$test
Return boolean test result(!!) of the given value
- params:
[any, string]
- returns
boolean
Other Keywords
Operators
$abs, $add(+), $sub(-), $mul(*), $div(/), $mod(%), $ceil, $floor, $round, $trunc, $sign
Comparers
$gt(>), $gte(>=), $lt(<), $lte(<=), $eq(===), $eqv(==), $ne(!==), $nev(!=), $in, $ni
String Morph
$upper, $lower, $snake, $pascal, $camel, $dash, $plural, $singular
Utils
A utils is attached to each config instance. You can acces it by the getter __
:
// make a deep cloned copyconst copy = config__; // merge target object's copy into source object's copy deeplyconst merged = config__; // load environment variables, optionally inject them into process.envconst envs = config__; // parse config by json stringlet conf = config__; // load config by path(absolute or relative to working directory)conf = config__; // delete config referenced cache(the next time you reference this module, config file will be reloaded)config__;
You can also use utils without the config instance:
const utils = ;// NOTE: utils.desolve will be undefined
Webpack
To integrate into webpack, there is a built-in plugin:
const ConfigurePlugin = ;
webpack.config.js
plugins: filename: path ;
And import that config in your modules:
index.js
;
You may add a resolve alias to shorten the import:
webpack.config.js
resolve: alias: config: path
index.js
;
NOTE
By default the json config is loaded as string. It's also compatible with file-loader. If conflicts occur, you may need to bypass the specific loader(s) with include/exclude
filters. e.g.
test: /\.json$/ exclude: /\/config\.json$/ use: 'some-loader'
Test
npm test
License
See License