@smpx/cfg
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -110,3 +110,3 @@ declare module '@smpx/cfg' { | ||
export default cfg; | ||
export = cfg; | ||
} |
@@ -18,3 +18,3 @@ const fs = require('fs'); | ||
function isObject(obj) { | ||
return obj === Object(obj); | ||
return Object.prototype.toString.call(obj) === '[object Object]'; | ||
} | ||
@@ -21,0 +21,0 @@ |
{ | ||
"name": "@smpx/cfg", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Configuration management for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# cfg | ||
Configuration management for node.js | ||
Configuration management for node.js. | ||
Example config: | ||
```js | ||
// config.js | ||
module.exports = { | ||
db: { | ||
password: 'abcde', | ||
host: '127.0.0.1', | ||
}, | ||
port: 3000, | ||
hosts: ['127.0.0.1'], | ||
logsDir: `${__dirname}/logs`, | ||
$env_production: { | ||
port: 80, | ||
logsDir: '/home/app/logs', | ||
}, | ||
$env_test: { | ||
port: 5000, | ||
}, | ||
$env_CI: { | ||
db: { | ||
// Docker image hostname | ||
host: 'postgresql', | ||
}, | ||
}, | ||
}; | ||
``` | ||
Usage: | ||
```js | ||
const cfg = require('@smpx/cfg'); | ||
const dbConf = cfg('db'); // { password: 'abcde', host: '127.0.0.1' } | ||
const dbPassword = cfg('db.password'); | ||
``` | ||
## Docs | ||
From sm-utils: https://smartprix.github.io/sm-utils/config.html | ||
It reads values from `config.js` file from project directory, but they can be overwritten with another `config.js` in the `private` folder in the project directory. Or through env vars in this format: | ||
```sh | ||
# Overwriting password (db.password): | ||
CFG__DB__PASSWORD='password' yarn start | ||
# Adding host to posiition 1 (hosts.1): | ||
CFG__HOSTS_1='new-host.region.rds.amazonaws.com' yarn start | ||
``` | ||
> NOTE: ENV VARS override might only work with camelCase keys | ||
It basically uses lodash.set internally. The path is generated by removing the `CFG__` prefix and replacing `__` with `.` and converting each word in between to camelCase (also through lodash). | ||
### NODE_ENV & CI overrides | ||
cfg also allows overriding config according to NODE_ENV or CI environment variables. For example if NODE_ENV="production", then if a `$env_production` key exists it's value gets merged over existing conf (this happens before merging any `private/config.js` file). | ||
Similarly in CI environments, the value in `$env_CI` is merged. | ||
## API | ||
Please check out the typescript definition file: [index.d.ts](./index.d.ts) for a overwview of all the functions provided. | ||
## CLI | ||
@@ -14,4 +76,13 @@ | ||
```sh | ||
# Installed globally | ||
cfg get redis.port | ||
cfg get logsDir | ||
# See how ENV_VAR will override config | ||
CFG__DB__PASSWORD='password' cfg get "db.password" | ||
# Through npx or yarn (when installed locally) | ||
npx cfg get redis.port | ||
yarn cfg get logsDir | ||
``` |
18340
88