Giter VIP home page Giter VIP logo

confugu's Introduction

confugu

Simple config loading with support for injecting environment variables.

Installation

npm i confugu

Usage

First, set up a yaml file to hold your config values.

logLevel: debug

port: ${ HTTP_PORT } # use 'HTTP_PORT' environment variable

Next, just pass the path to your config file to the asynchronous load function.

const confugu = require('confugu')

;(async () => {
  const config = await confugu.load('path/to/your/config')

  console.log(config.logLevel) // prints 'debug'
  console.log(config.port) // prints whatever is the value of process.env.HTTP_PORT
})()

Or you can load your config synchronously using loadSync:

const confugu = require('confugu')

const config = confugu.loadSync('path/to/your/config')

console.log(config.logLevel) // prints 'debug'
console.log(config.port) // prints whatever is the value of process.env.HTTP_PORT

Safely fetching nested properties

Fetching a nested property safely without having to check existence all the way down the property chain is also supported using the get function:

const confugu = require('confugu')

const config = confugu.loadSync('path/to/your/config')

console.log(config.get('logLevel')) // prints the value of the 'logLevel' property
console.log(config.get('db.password')) // prints the value of the 'db.password' property
console.log(config.get('invalid.nested.property')) // Invalid properties are "undefined"

Running tests

npm test

confugu's People

Contributors

austinkelleher avatar ctdio avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

austinkelleher

confugu's Issues

Support a `get` function for fetching properties

Fetching nested properties can result in quite a bit of conditional checking for existence. For example:

my-config.yml

my:
  deeply:
    nested:
      name: 'John'

config-loader.js

const confugu = require('confugu')

const config = confugu.loadSync('./config-loader')

if (config.my && config.my.deeply && config.my.deeply.nested) {
  console.log(config.my.deeply.nested.name)
}

Other config loaders such as config and async-config support a get property on the config object to make this easier:

const confugu = require('confugu')

const config = confugu.loadSync('./config-loader')

console.log(config.get('my.deeply.nested.name')) // Returns undefined if the nested property does not exist

Support JSON

Just opening this issue to track our discussion on supporting JSON and YML. The downside to this is that syntax highlighting in text editors may break as well as JSON validators complaining that the data is invalid. This would make text editor plugins that automatically validate JSON (e.g. https://atom.io/packages/jsonlint) completely unusable for these files. They would always be complaining.

We have a few options here:

  1. Enforce that all confugu environment variables are encoded as strings in the JSON file:
{
   "numVar": "${ NUM_VAR }"
}

In this case, we would need to support escaping, so that you could genuinely use ${ ... } inside of your JSON files if you didn't intend for it to be replaced by confugu. More importantly, we will have to understand the type that is intended for the environment variable, so that the quotes can be removed if necessary.

Example:

NUM_VAR=1

Yields:

{
   "numVar": 1
}
  1. Alternatively, we could support a .confugu custom file extension (e.g. config.json.confugu or config.yml.confugu). We could do a simple replace and JSON.parse would handle the validation.
{
  "numVar": ${ NUM_VAR }
}

/cc @Druotic

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.