@iteam/config
Advanced tools
Comparing version 12.1.0 to 12.1.1
{ | ||
"name": "@iteam/config", | ||
"version": "12.1.0", | ||
"version": "12.1.1", | ||
"main": "src/index.js", | ||
@@ -22,2 +22,4 @@ "repository": { | ||
"devDependencies": { | ||
"@semantic-release/changelog": "3.0.4", | ||
"@semantic-release/git": "7.0.16", | ||
"chai": "4.2.0", | ||
@@ -24,0 +26,0 @@ "eslint": "6.0.1", |
206
README.md
@@ -1,106 +0,26 @@ | ||
# config | ||
# Config | ||
This is useful for when the environment-variables need to be nested and <br/> still be camel cased. | ||
[![](https://github.com/Iteam1337/config/workflows/Release/badge.svg)](https://github.com/Iteam1337/config/actions?workflow=Release) | ||
[![npm version](https://badge.fury.io/js/%40iteam%2Fconfig.svg)](https://badge.fury.io/js/%40iteam%2Fconfig) | ||
The order of how the config is beeing transformed is: | ||
This is useful when environment variables need to be nested and still be camel cased. | ||
- 0: defaults | ||
- 1: environment | ||
- 2: config-file | ||
- 3: secrets | ||
So that means that `environment-variables` will override `defaults`, <br/> | ||
and the `config-file` will override `environment-variables` and so on. | ||
## Documentation | ||
Full documentation is found at [Iteam Config](https://iteam1337.github.io/#/config/examples) | ||
Properties defined in defaults will _almost always_ be returned, but overridden. | ||
For example: | ||
```javascript | ||
defaults: { | ||
foo: { | ||
bar: 'baz', | ||
} | ||
} | ||
// application started with: | ||
// FOO__SOMETHING=else npm start | ||
console.log(config.get('foo')) // > { bar: 'baz', something: 'else' } | ||
console.log(config.get('foo:bar')) // > 'baz' | ||
console.log(config.get('foo:something')) // > 'else' | ||
## Installation | ||
``` | ||
Types defined in `defaults` will be applied to overridden values: | ||
```json | ||
// contents of config.json | ||
{ | ||
"another": [{ | ||
"key": "not hello", | ||
"value": "2", | ||
}] | ||
} | ||
npm install @iteam/config | ||
``` | ||
```javascript | ||
const options = { | ||
file: `${__dirname}/../config.json`, | ||
defaults: { | ||
some: { | ||
nested: { | ||
array: [ 1, 2, 3 ], | ||
} | ||
}, | ||
booleanValues: { | ||
zero: false, | ||
one: true | ||
}, | ||
another: [{ | ||
key: 'hello', | ||
value: 1, | ||
}], | ||
} | ||
} | ||
or use [`supreme`](https://github.com/Iteam1337/supreme) to install and set up config files automatically: | ||
// ... | ||
// application started with: | ||
// SOME__NESTED__ARRAY="4,5,6" BOOLEAN_VALUES__ZERO="true" BOOLEAN_VALUES__ONE="0" | ||
config.get('some') // > { nested: { array: [ 4, 5, 6 ] } } | ||
config.get('some:nested') // > { array: [ 4, 5, 6 ] } | ||
config.get('some:nested:array') // > [ 4, 5, 6 ] | ||
config.get('booleanValues') // { zero: true, one: false } | ||
config.get('booleanValues:zero') // true | ||
config.get('booleanValues:one') // false | ||
config.get('another') // [{ key: 'not hello', value: 2 }] | ||
``` | ||
**The casing is always ignored** *as an input*, but the values fetched will always be camel-cased. | ||
For example (starting application in **shell**): | ||
```sh | ||
SOME__NESTED__CONFIG__IN_CAMEL_CASE=ok \ | ||
some__nested__secondValue=bar \ | ||
npm start # or whichever entrypoint | ||
npx @iteam/supreme add config | ||
``` | ||
... will be be transformed into (result in **javascript**): | ||
## Simple usage | ||
```javascript | ||
{ | ||
some: { | ||
nested: { | ||
config: { | ||
inCamelCase: 'ok', | ||
}, | ||
secondValue: 'bar', | ||
} | ||
} | ||
} | ||
``` | ||
## simple usage | ||
```javascript | ||
const config = require('@iteam/config')({ | ||
@@ -112,103 +32,9 @@ file: `${__dirname}/../config.json`, | ||
}, | ||
baz: [ 1, 2, 3 ], | ||
} | ||
baz: [1, 2, 3], | ||
}, | ||
}) | ||
// `config` also has a _getter_ for `defaults` | ||
// this will override the previous defaults | ||
config.defaults = { foo: { bar: 'baz' }, baz: [ 1, 2, 3 ] } | ||
console.log(config.get('foo')) // > { bar: 'baz' } | ||
console.log(config.get('foo:bar')) // > 'baz' | ||
console.log(config.get('baz')) // > [ 1, 2, 3 ] | ||
config.get('foo') // { bar: 'baz' } | ||
config.get('foo:bar') // 'baz' | ||
config.get('baz') // [ 1, 2, 3 ] | ||
``` | ||
**defaults can be passed to the initial function-call** | ||
```javascript | ||
const config = require('@iteam/config')({ | ||
defaults: { | ||
foo: { | ||
bar: 'bar' | ||
}, | ||
baz: 'results' | ||
} | ||
}) | ||
``` | ||
## "secrets" | ||
This module got extended with `docker-swarm` in mind, and their way of handling <br/> secrects (which is run-time mounted files). | ||
There's a option for the config-module to look into a directory and treat all <br/> files a key/value config. | ||
For example: | ||
The directory `/run/secrets` has these file in it: | ||
```sh | ||
some_nested__file | ||
some_nested__other_file | ||
``` | ||
When they are provided to the module they will be transformed as such: | ||
```javascript | ||
{ | ||
someNested: { | ||
file: 'contents of "/run/secrets/some_nested__file"', | ||
otherFile: 'contents of "/run/secrets/some_nested__other_file"' | ||
} | ||
} | ||
``` | ||
To enable this, provide `secrets` as a argument when calling the module: | ||
```javascript | ||
const config = require('@iteam/config')({ | ||
secrets: { | ||
dir: '/run/secrets/' | ||
} | ||
}) | ||
// `config` also has a _getter_ for `secrets`: | ||
config.secrets = { | ||
dir: '/run/secrets/' | ||
} | ||
``` | ||
# arguments: | ||
- **defaults** takes an `object` | ||
```typescript | ||
{ | ||
search: boolean // default: 'false' | ||
dir: string // default: '../' | ||
file: string // default: 'config.json' | ||
} | ||
``` | ||
- **secrets** takes an `object` or a `string` | ||
When providing a string, this should be the directory where secrets are stored. <br/> The options-objects takes two properties: | ||
```typescript | ||
{ | ||
dir: string // default: '/run/secrets/' | ||
separator: string // default: '__' | ||
} | ||
``` | ||
- **env** takes an `object` | ||
```typescript | ||
{ | ||
separator: string // default: '__' | ||
} | ||
``` | ||
- **file** takes an `object` or a `string` | ||
When providing a string, this should point to the full location of the config-file, or the `dir` will be de default | ||
```typescript | ||
{ | ||
search: boolean // default: false | ||
dir: string // default: '../' | ||
file: string // default: 'config.json' | ||
} | ||
``` |
@@ -0,1 +1,2 @@ | ||
const fs = require('fs') | ||
const utils = require('./utils') | ||
@@ -5,2 +6,10 @@ const secrets = require('./secrets') | ||
const useSecrets = () => { | ||
if (!isDocker) { | ||
return false | ||
} | ||
return !fs.readFileSync('/proc/self/cgroup', 'utf8').includes('kubepods') | ||
} | ||
module.exports = options => { | ||
@@ -34,3 +43,3 @@ const _conf = new WeakMap() | ||
constructor ({ env = {}, file, secrets = isDocker ? Config.secrets() : false, defaults } = {}) { | ||
constructor ({ env = {}, file, secrets = useSecrets() ? Config.secrets() : false, defaults } = {}) { | ||
this.env = env | ||
@@ -37,0 +46,0 @@ this.file = typeof file === 'string' ? { file } : file |
@@ -15,2 +15,6 @@ const fs = require('fs') | ||
if (!fs.lstatSync(dir).isDirectory()) { | ||
return {} | ||
} | ||
const fileNames = fs.readdirSync(dir) | ||
@@ -17,0 +21,0 @@ const output = {} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20
599
17976
12
40
6