npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stefcud/configyml

v1.3.2

Published

simple and smart Yaml configurator for NodeJs applications

Downloads

2,257

Readme

ConfigYml

configYml

Simple and smart Yaml configurator for NodeJs applications

THIS IS A FORK OF THE ORIGINAL IS ABANDONED REPOSITORY

improved version by @stefanocudini

  • support basepath to search configs
  • removed moment dependecy
  • pass more options by object
  • defaultsEnv object to set defaults value for undefined process.env variables
$ npm install @stefcud/configyml --save

Install

$ npm install @stefcud/configyml --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('@stefcud/configyml')();

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

Custom base path

Set the base path to search config.yml, search to parent folder

const config = require('@stefcud/configyml')({basepath: '..', defaultsEnvVars: {DBUSER: 'guest'}})

Custom filename

load specific config file for any project

const namespace = 'projectname';
const config = require('@stefcud/configyml')({configfile: `config.${namespace}.yml`})

Substitution

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


defaultsEnvVars:
  DBNAME: 'mydb'

dns: myapp.com

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

db:
    location: mysql-db-prod
    user: ${DBUSER}
    dbname: ${DBNAME}

if $DBUSER environment var is not defined use default value inside defaultsEnvVars on instance options if $DBNAME environment var is not defined use default value inside defaultsEnvVars on config.yml option

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. NODE_ENV 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('@stefcud/configyml').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: NODE_ENV

Set the Environment ID using NODE_ENV process environment variable.

export NODE_ENV=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('@stefcud/configyml')();

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('@stefcud/configyml')();

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