Giter VIP home page Giter VIP logo

config's Introduction

config-yml - Simple Yaml Config for Node.js

Travis Build Coverage Status JavaScript Style Guide config-yml

Install

$ yarn add config-yml

or

$ npm install config-yml --save

Usage

Use config for yaml config files in Node.js projects. For example you might have a project with the following config.yml file in the project dir.

app:
    url: http://myapp.com/home
    cache: redis

db:
    location: mysql-db-prod

This config can be accessed like this.

const config = require('config-yml');

console.log(config.app.url);
console.log(config.app.cache);
console.log(config.db.location);

Substitution

You can substitute variables in the config.yml like this.

dns: myapp.com

app:
    url: http://${dns}/home
    cache: redis

db:
    location: mysql-db-prod

This config would yield the following.

console.log(config.app.url);

// outputs - http://myapp.com/home

Config Folder

Instead of having a file named config.yml with all of your environment settings in place, you could have a config folder at the root level of your project. This module will read in every .yml file, and return an object that looks like:

{
    [file-name]: [parsed-file-contents],
    ...,
} 

if you need to do cross-file referencing, you can, via dot-notation:

# file `a.yml`
foo: bar
#file `b.yml`
baz: ${a.foo}

will get you

{
    a: {foo: 'bar'},
    b: {baz: 'bar'}
}

Environment Specific Settings

Based on an Environment ID, you can designate specific override settings for different types of environments. First you have to specify your Environment ID. You can do so in one of several ways. The first Environment ID that is found in the following order wins.

  1. --env command line argument
  2. --${static-environment} command line argument
  3. ENVIRONMENT_ID process environment setting
  4. git branch name with regex filtering

Static Environments

To understand this better let's first talk about Static Environments. These are environments that have their own environment specific settings or Environment Overrides. Not necessarily all environments have their own environment specific settings, but those that do should be defined as Static Environments in the config.yml as follows:

environments:
    static:
        - dev
        - test
        - prod

Keys as environments

The other approach you can take is to have top level keys that only consist of your environments.

Using a single config.yml file

setup your config.yml as follows:

dev:
    # ...
test:
    # ...
prod:
    # ...

Using a Config folder.

Your filenames determine the keys, so your directory could be set as follows:

config/dev.yml
config/test.yml
config/prod.yml

Environment ID: load Argument

Set the Environment ID using the load function.

const config = require('config-yml').load('myenvironment')

Environment ID: --env Argument

Set the Environment ID using --env command line argument.

node app.js --env feature-xyz

This is often helpful when running gulp tasks.

gulp deploy --env feature-xyz

Environment ID: --${static-environment} Argument

For Static Environments set the Environment ID using the static environment id as an argument.

gulp deploy --prod

Environment ID: ENVIRONMENT_ID

Set the Environment ID using ENVIRONMENT_ID process environment variable.

export ENVIRONMENT_ID=feature-xyz

Environment ID: git branch

If an Environment ID is not found using one of the other methods, it will use the git branch for the current project folder. This branch can be filtered using regex. Let's say your current branch is Features/ISSUE-123-feature-xyz, and you have the following setting in your config.yml.

branchRegex: Features/ISSUE-\d+-((\w|-)+)

The Environment ID will be feature-zyz. If no branchRegex is given the branch name will be taken as is.

Environment ID Substitution

The Environment ID can be substituted into the config.yml. Let's say you have an Environment ID feature-xyz and the following config.yml.

dns: ${envId}.myapp.com

app:
    url: http://${dns}/home
    cache: redis

db:
    location: MYSQL-DB-${ENVID}

This will yield the following:

const config = require('config-yml');

console.log(config.dns);          // feature-xyz.myapp.com
console.log(config.app.url);      // http://feature-xyz.myapp.com
console.log(config.db.location);  // MYSQL-DB-FEATURE-XYZ

Environment Overrides

For Static Environments, settings can be overridden for that specific environment. For example, with the following config.yml:

dns: ${envId}.myapp.com

app:
    url: http://${dns}/home
    cache: redis

db:
    location: MYSQL-DB-${ENVID}

prod:
    app:
        url: https://${dns}
    db:
        location: DB-${ENVID}

and the following app.js file:

const config = require('config-yml');

console.log(config.dns);
console.log(config.app.url);
console.log(config.app.cache);
console.log(config.db.location);

the following command:

node app.js --prod

would output the following:

prod.myapp.com
https://prod.myapp.com
redis
MYSQL-DB-PROD

config's People

Contributors

andyday avatar gwsii avatar jollyromp avatar mollases avatar tmwllc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

config's Issues

Multiple warnings ('... accessing non-existent properties of module exports')

'Warning: Accessing non-existent property 'cat' of module exports inside circular dependency' - Just the first of many warnings of all the properties of 'module exports'.

Occurs in Node 14.4.0 (latest stable build at time of reporting). Does not occur in Node 11.11.0, so somewhere inbetween Node has changed.

First noticed the warnings in my own project after updating Node. Checked the package's tests. Reproduced.

using process.env to distinguish config files

Hi all,
I'm using PM2 to deploy my node app. It's not convenient for me to pass --dev / --prod in the command line. But I'm setting development/production for NODE_ENV`` variable. Will config-yml select the correct config file under `/config folder` corresponding to NODE_ENV``` value?

overriding lists

currently, if the config file has

testList:
  - 1
  - 2
  - 3
  - 4


alpha:
  testList:
    - 5
    - 6

normally, you'd get the 4 elements ([1, 2, 3, 4]), if you override with --alpha, you end up with [5, 6, 3, 4]. I expected to end up with just [5, 6].

How would I specify that I want something in every case except for 1?

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.