2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
An INI format parser & serializer.
|
|
|
|
|
|
|
|
## Note
|
|
|
|
|
|
|
|
- Sections are treated as nested objects.
|
|
|
|
|
|
|
|
- Section-less items are treated as globals.
|
2024-02-09 00:38:41 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
Consider an INI file such as the following:
|
|
|
|
|
2024-02-09 00:38:41 +00:00
|
|
|
```ini
|
2024-06-03 19:44:25 +00:00
|
|
|
; This comment is being ignored
|
|
|
|
scope = global
|
|
|
|
|
|
|
|
[database]
|
|
|
|
user = dbuser
|
|
|
|
password = dbpassword
|
|
|
|
database = use_this_database
|
|
|
|
|
|
|
|
[paths.default]
|
|
|
|
datadir = /var/lib/data
|
|
|
|
array[] = first value
|
|
|
|
array[] = second value
|
|
|
|
array[] = third value
|
2024-02-09 00:38:41 +00:00
|
|
|
```
|
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
You can **read**, **modify** and **write** it like so:
|
2024-02-09 00:38:41 +00:00
|
|
|
|
|
|
|
```js
|
2024-06-03 19:44:25 +00:00
|
|
|
import { writeFile , readFile } from 'node:fs/promises'
|
|
|
|
import { stringify , parse } from 'ini'
|
|
|
|
|
|
|
|
// Read INI file as text
|
|
|
|
|
|
|
|
let text = await readFile(`./Original.ini`,{
|
|
|
|
encoding : 'utf-8'
|
|
|
|
})
|
|
|
|
|
|
|
|
// Parse text data to object
|
|
|
|
|
|
|
|
const config = parse(text)
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
// Modify data object
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
config.scope = 'local'
|
|
|
|
config.database.database = 'use_another_database'
|
|
|
|
config.paths.default.tmpdir = '/tmp'
|
|
|
|
delete config.paths.default.datadir
|
|
|
|
config.paths.default.array.push('fourth value')
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
// Stringify data object
|
|
|
|
|
|
|
|
text = stringify(config,{
|
|
|
|
section : 'section'
|
|
|
|
})
|
|
|
|
|
|
|
|
// Write INI file as text
|
|
|
|
|
|
|
|
await writeFile(`./Modified.ini`,text)
|
2024-02-09 00:38:41 +00:00
|
|
|
```
|
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
The written file will contain the following:
|
2024-02-09 00:38:41 +00:00
|
|
|
|
|
|
|
```ini
|
2024-06-03 19:44:25 +00:00
|
|
|
[section]
|
|
|
|
scope=local
|
|
|
|
[section.database]
|
|
|
|
user=dbuser
|
|
|
|
password=dbpassword
|
|
|
|
database=use_another_database
|
|
|
|
[section.paths.default]
|
|
|
|
tmpdir=/tmp
|
|
|
|
array[]=first value
|
|
|
|
array[]=second value
|
|
|
|
array[]=third value
|
|
|
|
array[]=fourth value
|
2024-02-09 00:38:41 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
### Parse
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
Attempts to turn the given INI string into a nested data object.
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
```js
|
|
|
|
// You can also use `decode`
|
|
|
|
const object = parse(`<INI Text>`)
|
|
|
|
```
|
|
|
|
|
|
|
|
### Stringify
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
Encodes the given data object as an INI formatted string.
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
```js
|
|
|
|
// You can also use `encode`
|
|
|
|
stringify(object,{
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
/**
|
|
|
|
* Whether to insert spaces before & after `=`
|
|
|
|
*
|
|
|
|
* Disabled by default to have better
|
|
|
|
* compatibility with old picky parsers.
|
|
|
|
*/
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
whitespace : false ,
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
/**
|
|
|
|
* Whether to align the `=` character for each section.
|
|
|
|
* -> Also enables the `whitespace` option
|
|
|
|
*/
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
align : false ,
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
/**
|
|
|
|
* Identifier to use for global items
|
|
|
|
* and to prepend to all other sections.
|
|
|
|
*/
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
section ,
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
/**
|
|
|
|
* Whether to sort all sections & their keys alphabetically.
|
|
|
|
*/
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
sort : false ,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to insert a newline after each section header.
|
|
|
|
*
|
|
|
|
* The TOSHIBA & FlashAir parser require this format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
newline : false ,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Which platforms line-endings should be used.
|
|
|
|
*
|
|
|
|
* win32 -> CR+LF
|
|
|
|
* other -> LF
|
|
|
|
*
|
|
|
|
* Default is the current platform
|
|
|
|
*/
|
|
|
|
|
|
|
|
platform ,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to append `[]` to array keys.
|
|
|
|
*
|
|
|
|
* Some parsers treat duplicate names by themselves as arrays
|
|
|
|
*/
|
|
|
|
|
|
|
|
bracketedArray : true
|
|
|
|
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
*For backwards compatibility any string passed as the*
|
|
|
|
*options parameter is treated as the `section` option.*
|
2024-02-09 00:38:41 +00:00
|
|
|
|
|
|
|
```js
|
2024-06-03 19:44:25 +00:00
|
|
|
stringify(object,'section')
|
2024-02-09 00:38:41 +00:00
|
|
|
```
|
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
### Un / Escape
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
Turn the given string into a safe to
|
|
|
|
use key or value in your INI file.
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
```js
|
|
|
|
safe(`"unsafe string"`) // -> \"unsafe string\"
|
|
|
|
```
|
2024-02-09 00:38:41 +00:00
|
|
|
|
2024-06-03 19:44:25 +00:00
|
|
|
Or reverse the process with:
|
|
|
|
|
|
|
|
```js
|
|
|
|
unsafe(`\\"safe string\\"`) // -> "safe string"
|
|
|
|
```
|