cfg-reader
TypeScript icon, indicating that this package has built-in type declarations

3.0.3 • Public • Published

cfg-reader

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

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

Package Sidebar

Install

npm i cfg-reader

Weekly Downloads

21

Version

3.0.3

License

ISC

Unpacked Size

57.5 kB

Total Files

14

Last publish

Collaborators

  • timo92