项目作者: Timo972

项目描述 :
alt:V config (.cfg) file parser
高级语言: TypeScript
项目地址: git://github.com/Timo972/cfg-reader.git
创建时间: 2020-03-17T14:04:28Z
项目社区:https://github.com/Timo972/cfg-reader

开源协议:MIT License

下载


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

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)

  1. 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

  1. const { Config } = require("cfg-reader");
  2. const myCfg = new Config("config.cfg");
  3. const val = myCfg.get("test");
  4. //with the get method you can easiely filter the lines you need
  1. import { Config, Type } = from "cfg-reader";
  2. const testCfg = new Config("test.cfg");
  3. // If you know which type the value you want to get has you can use
  4. // getOfType(key: string, type: number).
  5. // It directly converts the value to the specific type
  6. // and does not have to iterate over all possible types.
  7. // -> little faster
  8. const myString = testCfg.getOfType<string>("test");
  9. // typeof myString === "string";

API

Check out Typescript types

Example

config.cfg

  1. mysql: {
  2. host: 127.0.0.1,
  3. user: root,
  4. password: test123,
  5. database: db
  6. }

index.js

  1. const mysql = require('mysql2');
  2. const config = new require('cfg-reader').Config('config.cfg');
  3. // equal to es6
  4. // import { Config } from 'cfg-reader';
  5. // const config = new Config('config.cfg');
  6. const con = mysql.createConnection(config.get('mysql'));
  7. // or
  8. const con = mysql.createConnection(config.config.mysql);
  9. ...