@tib/configload
v0.3.0
Published
Load configuration file in various formats: yaml, json5, json, toml and js
Downloads
4
Maintainers
Readme
@tib/configload
Load configuration file in various formats: yaml, json5, json, toml and js.
Supported Formats
- yaml
- json
- json5
- toml (install @iarna/toml manually in your package)
Installation
npm i @tib/configload
Usage
import {load, loadSync, autoLoad, autoLoadSync} from '@tib/configload';
// load sync
console.log(loadSync('foo.yml'));
console.log(loadSync('foo.yaml'));
console.log(loadSync('foo.json'));
console.log(loadSync('foo.json5'));
console.log(loadSync('foo.toml'));
// load async
(async () => {
console.log(await load('foo.yml'));
})();
// auto load sync
console.log(autoLoadSync('foo'));
// auto load async
(async () => {
console.log(await autoLoad('foo'));
})();
API
detect(file: string, lang?: string): {file: string, lang: string, loader: Loader} | undefined
detect config file with lang or default loaders
Params
file: string
- path of the file or prefix to read.lang?: string
- force using specified lang loader.return: {file: string, lang: string, loader: Loader} | undefined
- detected result
Example
import {detect} from '@tib/configload';
// Suppose we have the following files
//
// ├─┬ config
// ├── foo.yml
// ├── foo.json
// └── foo.toml
console.log(detect('foo')); // => {file: '<..>/foo.yml', lang: 'yaml', loader: <YamlLoader>}
console.log(detect('foo.json')); // => {file: '<..>/foo.json', lang: 'json', loader: <JsonLoader>}
console.log(detect('foo', 'toml')); // => {file: '<..>/foo.toml', lang: 'toml', loader: <TomlLoader>}
console.log(detect('foo', 'js')); // => undefied
console.log(detect('foo.json', 'toml')); // => undefied
load(filepath: string, options: LoadOptions = {}): Promise
load config file async
Params
filepath: string
- path of the file to read.options: LoadOptions
- options to pass to loaderreturns: Promise<Object>
- promised JSON
Example
import {load} from '@tib/configload';
(async () => {
console.log(await load('foo.yml'));
console.log(await load('foo.json'));
})();
loadSync(filepath: string, options: LoadOptions = {}): any
load config file sync
Params
filepath: string
- path of the file to read.options: LoadOptions
- options to pass to loaderreturns: Object
- JSON
Example
import {loadSync} from '@tib/configload';
console.log(loadSync('foo.yml'));
console.log(loadSync('foo.json'));
autoLoad(filepath: string, options: LoadOptions = {}): Promise
Async auto detect config file with the filepath
and load the config file
Params
filepath: string
- path of the file to read.options: LoadOptions
- options to pass to loaderreturns: Promise<Object>
- promised JSON
Example
import {autoLoad} from '@tib/configload';
(async () => {
console.log(await autoLoad('foo.yml'));
console.log(await autoLoad('foo.json'));
})();
autoLoadSync(filepath: string, options: LoadOptions = {}): any
Sync auto detect config file with the filepath
and load the config file
Params
filepath: string
- path of the file to read.options: LoadOptions
- options to pass to loaderreturns: Object
- JSON
Example
import {autoLoadSync} from '@tib/configload';
console.log(autoLoadSync('foo.yml'));
console.log(autoLoadSync('foo.json'));
LoadOptions
YamlLoadOptions
lang?: string
- The language to force usingencoding?: BufferEncoding
- Pass to fs.readFileflag?: OpenMode
- Pass to fs.readFilethrows?: boolean
- Throw a wrapped error if parse throwsfilename?: string
- Pass to js-yaml
JsonLoadOptions
lang?: string
- The language to force usingencoding?: BufferEncoding
- Pass to fs.readFileflag?: OpenMode
- Pass to fs.readFilethrows?: boolean
- Throw a wrapped error if parse throwsreviver?: Function
- A function that transforms the results. Pass to JSON.parse
TomlLoadOptions
lang?: string
- The language to force usingencoding?: BufferEncoding
- Pass to fs.readFileflag?: OpenMode
- Pass to fs.readFilethrows?: boolean
- Throw a wrapped error if parse throws
JsLoadOptions
lang?: string
- The language to force usingencoding?: BufferEncoding
- Pass to fs.readFileflag?: OpenMode
- Pass to fs.readFilethrows?: boolean
- Throw a wrapped error if parse throws