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

map-env-vars

v0.2.0

Published

Fill a variable map with values from ENV vars, selected based on current environment

Downloads

107

Readme

🔗 NPM Package

Map ENV Vars

Fill a variable map with values from ENV vars, selected based on current environment (defaults to process.env.NODE_ENV).

Installation

  1. yarn add map-env-vars or npm i map-env-vars

Usage

Let's examine an example:

example.js

import mapEnvVars from 'map-env-vars'

const mappedEnv = mapEnvVars({
  envConfig: {
    'remote_development': 'DEV_',
    'staging': 'STAGE_'
  },
  varLookups: {
    baseUrl: '{ENV}BASE_URL',
    enableDebug: '{ENV}ENABLE_DEBUG',
    host: 'HOST',
    port: 'PORT'
  }
})

console.log(mappedEnv)

For the remote_development environment, ENV variable names are expected to be prefixed with STAGE_ where ever the {ENV} token is used. For the staging environment, ENV variable names are expected to be prefixed with STAGE_ where ever the {ENV} token is used.

This behavior is defined by the key-value pairs of envConfig.

Note that the HOST and PORT ENV variables are searched for directly, without prefix, because the {ENV} token is omitted from their varLookups definition. This behavior also occurs whenever your current execution ENV is not found as a key in the envConfig object. In these cases, the {ENV} token is ignored during lookup.

In a staging NODE_ENV, w/ prefix defined in envConfig options, w/ properly prefixed variable names

NODE_ENV=staging STAGE_BASE_URL=https://test.com STAGE_ENABLE_DEBUG=true HOST=localhost PORT=3000 node index.js

# {
#   "baseUrl": "https://test.com",
#   "enableDebug": "true",
#   "host": "localhost",
#   "port": 3000
# }

In a qa NODE_ENV (not defined in envConfig options)

NODE_ENV=qa BASE_URL=https://test.com ENABLE_DEBUG=true node index.js

# {
#   "baseUrl": "https://test.com",
#   "enableDebug": "true",
#   "host": undefined,
#   "port": undefined
# }

# Note how {ENV} token is ignored when performing lookups. This is because the current
# execution ENV is not found as a key in the envConfig object.
# There are also undefined values here because some ENV values were not set during invocation.

Options

mapEnvVars({
  // defaults to `process.env`
  envData: { myFakeEnvData: true },
  // defaults to `process.env.NODE_ENV`
  envCurrent: 'staging',
  // required
  envConfig: {
    'ENV_ID': 'ENV_REPLACEMENT_'
    // ...
  },
  // required
  varLookups: {
    friendlyVarName: '{ENV}_BIG_SCARY_VAR_NAME'
    // ...
  }
})