- 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.
npm i --save cfg-reader@latest
The cfg-reader is now a full typescript port of the open source alt-config parser from the altMP Team.
- Config::save() returns Promise
- Config::getOfType() uses generics and only accepts key as argument
const { Config } = require("cfg-reader");
const myCfg = new Config("config.cfg");
const val = myCfg.get("test");
//with the get method you can easiely filter the lines you need
import { Config, Type } = from "cfg-reader";
const testCfg = new Config("test.cfg");
// If you know which type the value you want to get has you can use
// getOfType(key: string, type: number).
// It directly converts the value to the specific type
// and does not have to iterate over all possible types.
// -> little faster
const myString = testCfg.getOfType<string>("test");
// typeof myString === "string";
Check out Typescript types
config.cfg
mysql: {
host: 127.0.0.1,
user: root,
password: test123,
database: db
}
index.js
const mysql = require('mysql2');
const config = new require('cfg-reader').Config('config.cfg');
// equal to es6
// import { Config } from 'cfg-reader';
// const config = new Config('config.cfg');
const con = mysql.createConnection(config.get('mysql'));
// or
const con = mysql.createConnection(config.config.mysql);
...