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

@intelcorp/config-builder

v2.0.0

Published

Environment Configuration Builder

Downloads

6

Readme

Environment Configuration Builder

Overview

The Environment Configuration Builder (a.k.a. config-builder) is a tool to help store and manage configuration options for application that can support multiple environments (i.e. production, development, test, etc). Custom environment config works by providing overriding values for settings that only apply on the requested environment. Settings can be organized in multiple modules or sets (i.e. WebServerConfig, CacheConfig, etc)

Installation

npm install --save @intelcorp/config-builder

API Reference

cn = new ConfigBuilder(opts)

Creates a new instance of ConfigBuilder.

  • opts.configPath: Required. Path to the directory where to find the configuration files for all environments.
  • opts.defaults: Name of the folder containging the default values for all environments. Defaults to __defaults__
  • opts.freeze: Indicates whether to freeze the returned config object or not. Defaults to true.
  • opts.cache: Indicates whether to use a cache. Defaults to true.

cfg = cb.build(env)

Returns a map (cfg) with configuration settings for the requested environment. The env environment must be defined as a subfolder of opts.configPath.

cfg.readEnvFile(filename)

Reads a file (sync) from the config directory if exists and return its contents. Otherwise reads it from the default config folder.

Basic Usage

On your application do:

var ConfigBuilder = require('@intelcorp/config-builder'),
    cb = new ConfigBuilder({path: PATH_TO_YOUR_CONFIG_FOLDER}),
    config = cb.build(NAME_OF_ENVIRONMENT_TO_LOAD);

Where: config will be an object with all the settings for your requested environment. config.readEnvFile(filename) is a helper function to load text files that you want to store with the config. config.ENV is the name of the loaded environment

Recommended Usage

  • Create the following directory structure in your app:
/config
   index.js
   /envs
     /__defaults__
        config.json
        otherFile.json
     /dev
        config.json
     /prod
        config.json

Where config/index.js contains:

'use strict';

var env = process.env.NODE_ENV || 'dev',
    ConfigBuilder = require('@intelcorp/config-builder'),
    cb = new ConfigBuilder({path: __dirname}),
    config = cb.build(env);

module.exports = config;

Then elsewhere in your application, when you want to use your config, just do:

var config = require(`./config`);

The /envs directory contains one subdirectory for each supported environment. The __defaults__ directory is where you define all the baseline/default settings that your application supports. config.json will be the main settings file.

To override any value for a particular environment, just create a new JSON file of the same name under the corresponding environment subdirectory, with only the settings you want to override. Any setting not explicitly defined on a particular environment, will use the value defined on the __defaults__ folder.

Any property defined in a file named config.json will be available as config.someSetting. Properties defined on files named differently (i.e. filename.json) will be available as filenameConfig.someSetting.

Using Environment Variables

For sensitive values, such as passwords and keys, it is not recommended to store them as plain text. In those cases, using environment variables is a more secure alternative.

To use env variables on your config files, define the value of a setting using the format:

{
	...
	"MY_PASS": "$env:MY_PASS",
	...
}

Where MY_PASS is the name of an environment variable on your system.

Using .env Files

You can also define environment variables using a .env file placed on your config path. Whenever this file is present Config-Builder will read it an use it to create environment variables that can be used only for the current instance of your application and that can be used in your config files. This is specially useful for local development environments where you may want to have more control over the environment variables your application sees. It is highly recommented to NEVER commit your .env files to your source control repository, as they may contain sensitive values

Env files should be structured as sinple JSON key/value maps:

{
   ...
   "SOME_VAR":"SOME_VALUE"
}