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

magiconfig

v0.1.1

Published

Yet another Node.js config utility :-)

Downloads

2

Readme

Magiconfig

npm version

This is a tool for building app configuration based on the environment that it's run in. Magiconfig does the work of validating your config for you and is flexible enough to allow you as little or as much control over it as you would like. Note: Each validation step throws an error upon failing. For instance, if you haven't set NODE_ENV you will get the following: 'NODE_ENV is not set!'

Installation

npm i magiconfig --save

Usage

const magiconfig = require('magiconfig')

const config = magiconfig(params)

Magiconfig accepts a configuration object with the following properties:

  • envConfig - Key-value pairs of environment names and their respective config files
  • template - Path to config template file
  • mandatoryKeys - A list of keys that require values.
  • staticConfig - Path to static config file
  • doTypeValidation - Should we validate config types against the template?
  • validate - Custom validation function

Note: All paths are relative to project root.

envConfig

An object with key-value pairs of environment names and their config files(can be either .toml, .yml, .yaml, .json or .js files). i.e.

{
    dev: '/path/to/your/file'
    prod: '/path/to/some/other/file'
}

Your environment is determined based on NODE_ENV. For instance, you can set it this way through package.json:

...
"scripts": {
    "run-dev": "NODE_ENV=dev node app.js",
    "run-prod": "NODE_ENV=prod node app.js"
}
...

template(Optional)

Use this to validate a config file against a template file(can be either a .toml, .yml, .yaml, .json or .js file). The template file must itself be valid and contain all the keys that you wish the config file to have, including nested keys. The values are not used anywhere, so feel free to provide sample values and comments.

a = 'some string'
b = 1 # should be a number

[api]
hostname = 'someapi.com'
port = 1337
...

staticConfig(Optional)

Oftentimes an app has static values that do not change between different environments. The static config file will be merged with the environment config. Environment config parameters will override their static counterparts.

mandatoryKeys(Optional)

Use this to provide a list of mandatory keys(currently we only guard against empty strings). You can use dot notation for nested keys (i.e. ['api.hostname']). If you wish to mark all keys as mandatory simply use ['*']. However, if you still want to exclude some keys use the '-' prefix: ['*', '-api.port']

doTypeValidation(Optional)

(Works in conjunction with 'template') Validate the types of properties in our config as set in the config template. Supported types are:

  • number
  • boolean
  • string
  • array
  • object
  • enum

Example:

// config.js
const config = magiconfig({
    templateFile: 'template.toml',
    envConfig: {
        'prod': 'test4.toml'
    },
    doTypeValidation: true
})
    # template.toml
    a = 'string'
    b = 'number'
    c = 'array'
    d = 'object'
    e = 'enum: abc,def,ghi'
    # config.toml
    a = 'some srting'
    b = '123'
    c = [1, 2, 3]
    [d]
    e = 'Object property'
    f = 'Another object property'

validate (optional)

Additional custom validation function. Function accepts the final config and a callback:

    ...
    const fs = require('fs')

    validate: (config, cb) => {
        if (!fs.existsSync(config.path)) {
            // Invalid config
            return cb (new Error('Path does not exist!'))
        }

        // Valid config
        cb()
    }