A lightweight wrapper for js-yaml to easily read yml files
npm install yaml-readerThis is a lightweight reader of yaml files. It's only purpose is to read
yaml files to use its attributes in a node app. Therefore it keeps simple
and its dependencies low.
The asynchronous read provides promise based and callback based support, thus
you can choose the style you like better.
yaml-reader is written in ECMA 6 (Node version 8.11.1). Be sure your node version can handle
this or use babel for older node versions. No guarantee that everything works fine when using babel.
```
npm install --save yaml-reader
yaml-reader resolves the path from your project root onwards, i.e. lets assume the project structure given below.
Then you will call yamlReader.read('configs/app-config.yml') wherever you want to read the yaml file. Thus the callbin/app.js
will look the same in both and some/deep/project/path/someService.js. ``
+-- package.json
+-- README.md
+-- bin
+---- app.js
+-- some
+---- deep
+------ project
+-------- path
+---------- someService.js
+---- ...
+-- configs
+---- app-config.yml
#### Synchronous read
Read a yaml file synchronously. This is like requiring a json config file via require():
``
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml');
#### Asynchronous read
You can also read a yaml asynchronously. The result will be returned as a Promise or, if
a callback function is provided, with a callback.
##### Promise based:
`
const yamlReader = require('yaml-reader');
yamlReader.readAsync('path/from/project/root/to/yml.yml')
.then((config) => {
...
})
.catch((err) => {
...
})
`
##### Callback based:
`
const yamlReader = require('yaml-reader');
// without options, thus null as 2nd arg
yamlReader.readAsync('path/from/project/root/to/yml.yml', null, (err, config) => {
if (err) {
...
}
else {
...
}
})
`
In all cases you can pass in the encoding of the file with an option object. If this is not passed, utf8 will be used by default.
`
const options = {
encoding: yamlReader.constants.ENCODING.UTF_16_LE
};
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml', options);
// or async
yamlReader.readAsync('path/from/project/root/yml.yml', options, (err, config) => {
...
}
yamlReader.readAsync('path/from/project/root/yml.yml', options)
.then((config) => {
...
})
`
#### yamlReader.read(file [, options])
#### yamlReader.readYaml(file [, options])
Read a yaml file _synchronously_ .
- __file__ _\
to see how it is resolved if unclear
- __options__ _
``
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml');
Or with encoding specified:
``
const options = {
encoding: yamlReader.constants.ENCODING.UTF_8
};
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml', options);
#### yamlReader.readAsync(file [, options, callback])
#### yamlReader.readYamlAsync(file [, options, callback])
Read a yaml file _asynchronously_. The yaml attributes will be returned with a Promise.
- __file__ _\
to see how it is resolved if unclear
- __options__ _
The config is either returned as a _Promise\
`
const yamlReader = require('yaml-reader');
const options = { ... }
//
// awaiting promises
//
yamlReader.readAsync('path/from/project/root/to/yml.yml', options)
.then((config) => {
// access properties of your yaml
// console.log(config.myProperty)
})
.catch((err) => {
// handle errors (like FileNotFoundError)
})
//
// or with callback
//
yamlReader.readAsync('path/from/project/root/to/yml.yml', options, (err, config) => {
...
}
``