https://github.com/jrop/rc2
Load your rc files with ease - fully async
https://github.com/jrop/rc2
configuration configuration-files ini json json5 loader loader-configuration rc rc-files yaml yml
Last synced: about 1 month ago
JSON representation
Load your rc files with ease - fully async
- Host: GitHub
- URL: https://github.com/jrop/rc2
- Owner: jrop
- Created: 2017-05-31T20:42:35.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-01T17:34:18.000Z (almost 9 years ago)
- Last Synced: 2026-01-14T14:42:11.010Z (5 months ago)
- Topics: configuration, configuration-files, ini, json, json5, loader, loader-configuration, rc, rc-files, yaml, yml
- Language: TypeScript
- Homepage:
- Size: 44.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rc2
A follow-up to [rc](https://github.com/dominictarr/rc) that is more configurable.
*More configure! Such less bloat!*
## Features
* Fully asynchronous: no synchronous I/O in the module
* Looks only in locations/ranges of directories which you tell it to
* Loaders are **fully** configurable
* Zero-dependencies (all bundled)
## Example
```ts
import rc2 from 'rc2'
async function main() {
const loaders = rc2.loaders()
.default(['ini', 'json', 'js', 'yaml'])
.ini() // must have the `ini` module installed
.json() // uses `json5` module, if installed, otherwise uses `JSON.parse(...)`
.js() // uses `require(...)`
.yaml() // must have the `js-yaml` module installed
const config = await rc2({
name: 'myapp', // will look for '.myapprc' files
locations: [{bottom: __dirname}],
loaders,
})
}
main().catch(e => {
process.exitCode = 1
console.error(e)
})
```
## API
* `rc2(options) => Promise`
* `rc2.loaders() => Loaders`
`rc2(...)` will only look in the locations you specify (see options below) for rc files. In each directory, it searches the following globs:
* `.${name}rc`
* `.${name}rc.*`
* `.${name}/config`
* `.${name}/config.*`
* `.config/${name}`
* `.config/${name}.*`
* `.config/${name}/config`
* `.config/${name}/config.*`
* Environment variables (`${name}_some__property`)
* CLI args (parsed by minimist)
### Options
```ts
export interface Options {
name: string // the name of the app
locations?: { // locations to search for rc files
top?: string // if specified, denotes the "top" directory at which to stop looking
bottom: string // the start directory at which to start looking for rc files
}[]
loaders?: Loaders // an instance created with `rc2.loaders()`
default?: any // default configuration if none are found
argv?: string[]
env?: any
}
```
### Loaders
A loader instance can be created with `rc2.loaders()`. This instance has the following methods:
```ts
declare class Loaders {
add(ext: string, loader: (f: string) => any): this
default(exts: string[]): this
ini(): this
js(): this
json(): this
yaml(): this
}
```
#### Example `loaders.add(...)`:
```ts
rc2.loaders()
.add('xml', f => {
// Async is handled with `co`, so return
// any yieldable for this function to be async
return new Promise(...) // for example
})
```
For reference, [here is a list of valid `co` yieldables](https://github.com/tj/co#yieldables).