Giter VIP home page Giter VIP logo

gpcl's Introduction

npm Dependency Status Coverage Status Build Status MIT Licence

gpcl

A generic project configuration loader with a simple path-finder.

It searches for your config file and loads it.

No more someConfigLoader(resolve(__dirname, '..', '..', '..', 'config', 'main.yml')) !

Installation:

$ yarn add gpcl

Usage:

  1. Add a YAML file to your project directory (see here for default supported locations).
  2. Call loadConfigSync() in your script to access your config object.

Example:

src/backend/web/app/environments/uat/setup.js

import { loadConfigSync } from "gpcl";

const config = loadConfigSync();
// ...

.config/config.yml

IAC:
  Region: !env AWS_REGION
  Stage: !env APP_ENV

  Templates:
    Local: !path lib/blueprints
    BucketKey: infrastructure/blueprints

Development:
  ServerAddr: '0.0.0.0:4200'
  Hateoas: !path .meta/dev/docker_endpoints.json

Output:

{
  "IAC": {
    "Region": "ap-southeast-2",
    "Stage": "staging",
    "Templates": {
      "Local": "/Users/x/DevProjects/xo/lib/blueprints",
      "BucketKey": "infrastructure/blueprints"
    }
  },
  "Development": {
    "ServerAddr": "0.0.0.0:4200",
    "Hateoas": "/Users/x/DevProjects/xo/.meta/dev/docker_endpoints.json"
  }
}

API:

loadConfigSync(configPath?: string, rootDir?: string)

rootDir:

  • absolute path of your root project directory. If not specified, it will walk up the directory tree to find it. Indicators are:
    • package.json
    • node_modules

configPath:

  • absolute path of your config file. If not specified, it will look for your config file in this order (first in <rootDir>, then in <rootDir>/config and vice versa):

    • <rootDir[/.config, /config]>/config.yml
    • <rootDir[/.config, /config]>/project.yml
    • <rootDir[/.config, /config]>/settings.yml

Both params are optional. If you'd like to specify rootDir without specifying configPath, just pass in undefined as the first parameter.

Note: an error will be thrown if both values are not specified AND cannot be inferred.

Supported YAML Types:

Supports anything that is parsable by js-yaml, plus 2 additional custom types:

  1. !env type
    • for referencing an environment variable
    • I.E. process.env[!env]
    • E.G. !env NODE_ENV => 'production'
  2. !path type
    • for referencing a path relative from the root directory
    • I.E. <rootDir>/<!path>
    • E.G. !path src/app.js => /Users/xo/web/src/app.js

Type Definitions:

This comes bundled with its own type definitions for TypeScript, so you'll get auto-complete and hints if you're using the Visual Studio Code IDE.

Todos:

  • complete all tests
  • bump npm package to v1
  • write and test for Go
  • generate godocs
  • set go package version

Remarks

I created this after repetitively copy-pasting the same code across various private side-projects for loading configuration, and it became tedious to use path.resolve(__dirname, '..', '..', '..' /* etc */) on the same config file when calling it from multiple places in the codebase.

If this does not fit your needs, feel free to fork or contribute!

gpcl's People

Contributors

gerardmrk avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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