An INI format parser & serializer.
Note
Usage
Consider an INI file such as the following:
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
You can read, modify and write it like so:
import { writeFile , readFile } from 'node:fs/promises'
import { stringify , parse } from 'ini'
let text = await readFile(`./Original.ini`,{
encoding : 'utf-8'
})
const config = parse(text)
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')
text = stringify(config,{
section : 'section'
})
await writeFile(`./Modified.ini`,text)
The written file will contain the following:
[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
API
Parse
Attempts to turn the given INI string into a nested data object.
const object = parse(`<INI Text>`)
Stringify
Encodes the given data object as an INI formatted string.
stringify(object,{
whitespace : false ,
align : false ,
section ,
sort : false ,
newline : false ,
platform ,
bracketedArray : true
})
For backwards compatibility any string passed as the
options parameter is treated as the section
option.
stringify(object,'section')
Un / Escape
Turn the given string into a safe to
use key or value in your INI file.
safe(`"unsafe string"`)
Or reverse the process with:
unsafe(`\\"safe string\\"`)