Repository
Current version released
2 years ago
Versions
cfg-reader
License and copyright notice
Important
- supported node version >= 12
- Since v2.1.0 you can use this module for the alt:V nodejs version and default nodejs version at the same time.
Installation (works for alt:V too)
npm i --save cfg-reader@latest
Differences between v3
The cfg-reader is now a full typescript port of the open source alt-config parser from the altMP Team.
Breaking changes
- Removed Config class API
How to use
# config.cfg
name: Test
port: 7788
players: 1
// config.ts
// Typescript types
import type { List, Dict, ConfigValue } from "cfg-reader";
// Functions
import { parse, serialize } from "cfg-reader";
import { promises: { readFile } } from 'fs';
const rawConfig = await readFile('config.cfg');
const config = parse(rawConfig);
console.log(config.port); // prints: 7788
console.log(config.players); // prints: 1
API
Check out Typescript types
Example
# config.cfg
mysql: {
host: 127.0.0.1,
user: root,
password: test123,
database: db
}
// index.js
const mysql = require('mysql2');
const { readFile } = require('fs').promises;
const CFG = require('cfg-reader');
// equal to es6
// import * as CFG from 'cfg-reader';
const rawConfig = await readFile('config.cfg');
const { mysql } = CFG.parse(rawConfig);
const conn = mysql.createConnection(mysql);
...