<div align="center">
<img src="https://github.com/roxiness/configent/raw/master/configent.png" alt="configent">
</div>

# Configent

### Confident configurations

No fuzz config compilation from (ordered by ascending precedence)

*   defaults
*   package.json
*   \[name].config.js
*   .env
*   environment
*   input

```javascript
/** 
 * package.json {"foobar": {"city": "Portsmouth"}}
 * foobar.config.js {lastSeen: 'Liverpool'}
 * process.env.foobar_last_seen = London
 * options = { name: 'Sherlock Holmes' }
*/

const defaults = { name: 'John Doe', city: 'N/A', lastSeen: 'N/A' }

const config = configent('foobar', defaults, options)

/**
 * console.log(config)
 * {
 *   name: 'Sherlock Holmes',
 *   city: 'Portsmouth',
 *   lastSeen: 'London'  
 * }
 * /
```

### Auto detect defaults

Configent supports multiple default configs. These are added to `./configs`.

```javascript
/** ./configs/routify2.config.js */

module.exports = {
    supersedes: ['svelte'],
    condition: ({ pkgjson }) => pkgjson.dependencies['@roxi/routify'],
    config: () => ({ 
        /** the config object used as default */
        myAppName: 'Routify App' 
    })
}
```

```javascript
/** ./configs/svelte.config.js */

module.exports = {
    condition: ({ pkgjson }) => pkgjson.dependencies['svelte'],
    config: () => ({ 
        /** the config object used as default */
        myAppName: 'Svelte App' 
    })
}
```

The first config with a true condition is used. To avoid conflicts, configs using the  `supersedes` option, will run before their superseded targets.

To change the location of default configs, refer to `detectDefaultsConfigPath`.

### API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

##### Table of Contents

*   [configent](#configent)
    *   [Parameters](#parameters)

#### configent

##### Parameters

*   `options` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** configent options

    *   `options.defaults` **{}** defaults. Used for type casting env variables (optional, default `{}`)
    *   `options.name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** name to use for configs. If left empty, name from package.json is used (optional, default `''`)
    *   `options.cacheConfig` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** calling configent twice with same parameters will return the same instance (optional, default `true`)
    *   `options.cacheDetectedDefaults` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** calling configent twice from the same module will return the same defaults (optional, default `true`)
    *   `options.useDotEnv` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** include config from .env files (optional, default `true`)
    *   `options.useEnv` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** include config from process.env (optional, default `true`)
    *   `options.usePackageConfig` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** include config from package.json (optional, default `true`)
    *   `options.useConfig` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** include config from \[name].config.js (optional, default `true`)
    *   `options.useDetectDefaults` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** detect defaults from context (package.json and file stucture) (optional, default `true`)
    *   `options.detectDefaultsConfigPath` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** detect defaults from context (package.json and file stucture) (optional, default `'configs'`)
    *   `options.sanitizeEnvValue` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** sanitize environment values. Convert snake_case to camelCase by default. (optional, default `str=>str.replace(/[-_][a-z]/g,str=>str.substr(1).toUpperCase())`)
    *   `options.module` **NodeModule?** required if multiple modules are using configent

####


---

<a href="https://www.freepik.com/vectors/vintage">Vintage vector created by macrovector - www.freepik.com</a>