# ut-config

> Reusable configuration `load` and `edit` methods.

Latest version **7.10.0** (published 2023-02-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install ut-config
pnpm add ut-config
yarn add ut-config
bun add ut-config
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 7.10.0 |
| Published | 2023-02-10 |
| First published | 2019-07-25 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 22.2 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | kalin.krustev |

## Links

- npm: https://www.npmjs.com/package/ut-config
- Repository: https://github.com/softwaregroup-bg/ut-config
- Homepage: https://github.com/softwaregroup-bg/ut-config#readme
- Issues: https://github.com/softwaregroup-bg/ut-config/issues
- npm.io page: https://npm.io/package/ut-config

## Dependencies (11)

- [rc](https://npm.io/package/rc.md) 1.2.8
- [ajv](https://npm.io/package/ajv.md) 8.6.3
- [ini](https://npm.io/package/ini.md) 1.3.6
- [yaml](https://npm.io/package/yaml.md) 1.10.0
- [minimist](https://npm.io/package/minimist.md) 1.2.6
- [ajv-formats](https://npm.io/package/ajv-formats.md) 2.1.1
- [ut-function.cbc](https://npm.io/package/ut-function.cbc.md) ^1.1.6
- [ut-function.merge](https://npm.io/package/ut-function.merge.md) ^1.5.6
- [ut-form-jsonschema](https://npm.io/package/ut-form-jsonschema.md) ^7.1.7
- [strip-json-comments](https://npm.io/package/strip-json-comments.md) 2.0.1
- [ut-function.template](https://npm.io/package/ut-function.template.md) ^1.7.3

## Recent versions

- 7.10.0 (latest) — 2023-02-10
- 7.9.3 — 2022-03-22
- 7.9.2 — 2021-09-05
- 7.9.1 — 2021-09-03
- 7.9.0 — 2021-09-02
- 7.8.0 — 2021-08-28
- 7.7.3 — 2021-06-18
- 7.7.2 — 2020-12-15
- 7.7.1 — 2020-11-24
- 7.7.0 — 2020-11-13
- 7.6.1 — 2020-10-28
- 7.6.0 — 2020-09-17
- 7.5.5 — 2020-07-13
- 7.5.4 — 2020-07-06
- 7.5.3 — 2020-04-06
- … 15 more at https://npm.io/package/ut-config/versions

## README

# ut-config

Reusable configuration `load` and `edit` methods.

## Starting

`ut-config` assumes the application is being run with a command like

```bash
node index {app} {method} {env}
```

or

```bash
UT_APP=server UT_METHOD=debug UT_ENV=dev node index
```

### Configuration

In addition to using environment configuration files within the application filesystem,
the following additional options are available, which will override the configuration

- Configuration file
- Command line parameters
- Environment variables

The algorithm of how these are applied is described in the `rc` package, [here](https://github.com/dominictarr/rc).
This is adapted from `rc` package readme:

- command line arguments, parsed by minimist _(e.g. `--foo baz`, also nested: `--foo.bar=baz`)_
- environment variables prefixed with `ut_${impl}_${env}_`
  - or use "\_\_" to indicate nested properties _(e.g.
  `ut_${impl}_${env}_foo__bar__baz` => `foo.bar.baz`)_
- if you passed an option `--config file` then from that file
- a local `.ut_${impl}_${env}rc` or the first found looking in
  `./ ../ ../../ ../../../` etc.
- `$HOME/.ut_${impl}_${env}rc`
- `$HOME/.ut_${impl}_${env}/config`
- `$HOME/.config/ut_${impl}_${env}`
- `$HOME/.config/ut_${impl}_${env}/config`
- `/etc/ut_${impl}_${env}rc`
- `/etc/ut_${impl}_${env}/config`
- the object taken from environment configuration file within {app} / UT_APP folder
  (dev.js[on], test.js[on], etc.)
- the object taken from method configuration file within {app} / UT_APP folder
  (debug.js[on], install.js[on])
- the object taken from common.js[on] configuration file within {app} / UT_APP folder

All configuration sources that were found will be flattened into one object,
so that sources **earlier** in this list override later ones.

File based configuration sources outside the application folder can be in
`ini`, `json` or `yaml` format.

${impl} is implementation identifier taken from environment configuration file,
${env} is the environment passed through command line or UT_ENV environment
variable, or 'dev' (by default)

### Environment configuration files

These files are part of the application and define the defaults
for each environment.
Environment configuration files can be either `.json` or `.js` file.
If it is a `.js` file, it must export an object. If a file named `common.js` or
`common.json` exists, it is used as a base object for all environments where the
actual environment configuration is merged. When using `.js` file, more complex
configuration is possible - like having functions or other objects, not
supported by JSON. Minimal environment file `dev.json` may look like:

```json
{
    "implementation": "impl-test",
    "service": "admin"
}
```

### Templating

After all configurations have been loaded and merged
into a common object it gets processed through a
template engine. For more info check
[ut-function.template](https://github.com/softwaregroup-bg/ut-function/tree/master/packages/template)
and in particular the provided
[recursive rendering](https://github.com/softwaregroup-bg/ut-function/tree/master/packages/template#recursive-rendering)
functionalities.

E.g.

```js
const obj = require('ut-config').load({
    config: {
        x: 'normal string',
        y: '${hello("Test1")}',
        z: '${hello(test)}'
    },
    context: {
        hello: who => 'Hello ' + who + ' !!!',
        test: 'Test2'
    }
});

// obj.y would be equal to 'Hello Test1 !!!'
// obj.z would be equal to 'Hello Test2 !!!'
```

Everything that is passed in the `context`
object is accessible as a context in the config through
the template engine.

The context is additionally enriched with
`encrypt` and `decrypt` methods (this is only in case
`UT_MASTER_KEY` is available as environment variable which
is currently being set by Jenkins).
So if you have the correct master key you can
securely define encrypted passwords and other sensitive
data in the config.

E.g.

```js
/*
  note that the context is not mandatory if you need to use
  only the built-in encrypt/decrypt methods
*/
const obj = require('ut-config').load({
    config: {
        pass: '${decrypt("3569bf662a23fad6eb2069e09e8da490dff37a84e69a5eb82a1efecf9f8fcdb2")}'
    }
});
```

---
_Source: https://npm.io/package/ut-config · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
