read-ini
Version:
Read and parse INI or ENV files
106 lines (75 loc) • 2.48 kB
Markdown
[](https://github.com/vitaly-t/read-ini/actions/workflows/ci.yml)
[](https://nodejs.org)
A simple reader and parser of `.ini` (or `.env`) text / files for NodeJS.
```
$ npm i read-ini
```
The API contains just two functions:
* [readIni] - takes `.ini`/`.env`-like text content, parses it and returns a JSON object.
* [readIniFile] - synchronously reads a text file and then forwards to [readIni].
The library supports sections, with aliases, but without nesting.
**INI file example**
```ini
SHARED_VALUE = some text
; full-line comment can start with ;
[]
DB_HOST = localhost
DB_PORT = 123
```
Reading the file above...
```ts
import {readIniFile} from 'read-ini';
readIniFile('./file.ini'); //=> JSON object
```
**Output:**
```js
{
SHARED_VALUE: 'some text',
database: {
DB_HOST: 'localhost',
DB_PORT: '123'
}
}
```
You can pass in an optional callback, as the second parameter, to convert values.
```ts
readIniFile('./file.ini', ({key, value, section}) => {
if (key === 'DB_PORT') {
return parseInt(value); // convert to integer
}
return value; // else return the value
});
```
**Output:**
```js
{
SHARED_VALUE: 'some text',
database: {
DB_HOST: 'localhost',
DB_PORT: 123
}
}
```
Optional section aliases are supported: `[section "alias"]`, and those simply replace the section name.
Section name `global` is reserved (case-insensitive), to inject variables into the global scope from anywhere inside an
INI file. The same works for any section with `global` as alias.
### Environment Variables
To set environment variables from the output, you can use this helper:
```ts
function setEnvironmentVars(vars: { [name: string]: any }): void {
for (const [name, value] of Object.entries(vars)) {
if (typeof value !== 'object') {
process.env[name] = value;
}
}
}
```
[]:https://github.com/vitaly-t/read-ini/blob/main/src/index.ts#L36
[]:https://github.com/vitaly-t/read-ini/blob/main/src/index.ts#L72