/cfg-reader

alt:V config (.cfg) file parser for nodejs & deno

Primary LanguageTypeScriptMIT LicenseMIT

cfg-reader

NPM Version NPM Downloads Node.js Version Codacy Badge Latest Build

Deprecation notice

⚠️ alt:V has moved to toml configuration files, so this package is obsolete

License and copyright notice

alt-config (MIT)

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 v2

The cfg-reader is now a full typescript port of the open source alt-config parser from the altMP Team.

Breaking changes

  • Config::save() returns Promise
  • Config::getOfType() uses generics and only accepts key as argument

How to use

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";

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 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);
...