项目作者: nyaaery

项目描述 :
Pretty self-explanatory
高级语言: TypeScript
项目地址: git://github.com/nyaaery/node-module-that-loads-configs.git
创建时间: 2019-01-10T11:06:06Z
项目社区:https://github.com/nyaaery/node-module-that-loads-configs

开源协议:

下载



(Character: Tohru from Miss Kobayashi’s Dragon Maid)

A better way to load your configuration files

Motivation

I needed a module to save me from having to write the same snippets of code in every project that required a config.
There already existed many modules that drastically reduced the amount of code I would have to write.
But I often still found myself in very simmilar situations: Like writing snippets to generate default configs.
Not to mention anyting to do with directories.
So that’s why I made this neat little module. I hope you are able to get a simmilar amount of value from it as I

Features

  • Simple, load your configs with a single line of code
  • Powerful, read and write both files and directories with defaults
  • Flexible, create support for formats of your needs
  • TypeScript, pre-packaged type definitions
  • JSDocs, together with TS type definitions enables helpful code completion in modern IDEs

Documentation

Examples

Reading a raw text file

  1. import * as mlc from "@aery/mlc";
  2. const file: mlc.ConfigFile = await mlc.file("raw.txt") // Defaults to RawFormat format by default
  3. .read();
  4. file.content;

Reading a JSON file

  1. import * as mlc from "@aery/mlc";
  2. const file: mlc.ConfigFile = await mlc.file("config.json") /* Associates json files with
  3. JSONFormat format by default */
  4. .defaults({
  5. ip: "127.0.0.1",
  6. port: 1337
  7. }) // What content should default to when reading
  8. .read({ write_if_defaulted: true }); // Write if content was defaulted in any way after reading
  9. file.content;

Writing a raw text file

  1. import * as mlc from "@aery/mlc";
  2. const file: mlc.ConfigFile = mlc.file("foo.txt"); // Defaults to RawFormat format by default
  3. file.content = "bar";
  4. file.write();

Reading a directory

  1. import * as mlc from "@aery/mlc";
  2. const directory: mlc.ConfigDirectory = await mlc.directory("recipies", new mlc.formats.JSONFormat())
  3. .defaults({
  4. "water.json": {
  5. steps: [ "Pour water" ]
  6. },
  7. "cereal.json": {
  8. steps: [
  9. "Pour cereal FIRST",
  10. "THEN pour milk"
  11. ]
  12. }
  13. }) // What the ConfigDirectory's ConfigFiles' content should default to when reading
  14. .read();
  15. const contents: object = directory.contents();
  16. contents["water.json"];
  17. contents["cereal.json"];

Compiling

npm install --only=dev
npm run-script compile

Testing

npm install --only=dev
npm run-script test