Coder Social home page Coder Social logo

gallant-config's Introduction

Config

Simple TypeScript/ES2017 class to load configuration files from a folder

Installation

npm install --save @gallant/config

Usage

import { Config } from '@gallant/config';

// Can either be a folder or a file name
const config = Config.load( 'config' );

// Can also use the async variant
const configAsync = Config.loadAsync( 'config' );

// Multiple configurations can also be deep merged manually
const fullConfig = Config.merge( [ config, configAsync ] );

// Getting values can be done through the .get method, with an optional default value
const value = fullConfig.get<OptionalType>( 'key.value', 'optional default value' );

By default the app uses the JSON loader. If your config files use another format, you can provide it a custom loader. For example, using the json5 module:

import { Config } from '@gallant/config';
import JSON5 from 'json5';

const config = Config.load( 'config', [ new class {
    accepts = ( name : string ) => /json(5)?/i.test(name);

    parse = JSON5.parse
} ] );

KDL

KDL is a document language with xml-like semantics, but a lighter syntax.

We also support loading KDL files out of the box, but those require a litle a schema to be defined, so that the file can be properly translated to an object in memory.

package {
    name "foo"
    version "1.0.0"
    dependencies platform="windows" {
        winapi "1.0.0" path="./crates/my-winapi-fork"
    }
    dependencies {
        miette "2.0.0" dev=true
    }
}
import { Config, SchemaUtils, KDLFormat } from '@gallant/config';
import {
    Tag, Any, Child, Children, Property, Value, Optional, Default
} from '@gallant/config/kdl';

export class PackageDependency {
    @Tag()
    name: string; // winapi, miette

    @Value(0, String)
    version: string; // 1.0.0, 2.0.0
    
    @Optional() @Property('dev', Boolean)
    dev?: boolean; // null, true

    @Optional() @Property('path', String)
    path?: string; // null, ./crates/my-winapi-fork
}

export class DependenciesGroup {
    @Optional() @Property('platform', String)
    platform?: string;

    @Default() @Children(Any, PackageDependency)
    packageDependencies: PackageDependency[] = [];
}

export class Package {
    @Child('name', String)
    name!: string;

    @Child('version', String)
    version!: string;

    @Default() @Children('dependencies', DependenciesGroup)
    dependencies!: DependenciesGroup[];
}

const format = new KDLFormat(SchemaUtils.schemaOf(Child('package', Package));

const package: Package = Config.load('package.kdl', format);

The resulting object from this will be:

{
  "name": "foo",
  "version": "1.0.0",
  "dependencies": [
    {
      "packageDependencies": [
        {
          "name": "winapi",
          "version": "1.0.0",
          "dev": false,
          "path": "./crates/my-winapi-fork"
        }
      ],
      "platform": "windows"
    },
    {
      "packageDependencies": [
        {
          "name": "miette",
          "version": "2.0.0",
          "dev": true,
          "path": null
        }
      ],
      "platform": null
    }
  ]
}

gallant-config's People

Contributors

pedromsilvapt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.