Git Product home page Git Product logo

environment-enforcer's Introduction

Environment Enforcer

Babel Macro

"When you want your code to work in every environment, call on the Environment Enforcer."

Usage / Examples

  1. run npm install environment-enforcer.macro
  2. Create a single file that will provide your code access to your environment files. Let's call it wrappedEnvVars.ts
  3. Import this macro and pass it the interface that defines all of the environment variables you require to be present at runtime.
// inside src/wrappedEnvVars.ts
import EnvironmentEnforcer from 'environment-enforcer.macro';

interface IExample {
  MY_API_URL: string;
}

export const envVars = EnvironmentEnforcer.parse<IExample>();
  1. create a folder called environments (or see Configuration below for other options) and place it at the level of package.json
  2. create files within this folder that match your environments. For example, if you don't change the default stageNames value, you would want to have 5 files in this folder. So:
package.json
environments/
environments/test.json
environments/development.json
environments/qa.json
environments/staging.json
environments/production.json
node_modules/
  1. The content of each of these files must adhere to the interface you passed into the macro in the step above. So for example, environments/development.json would be:
{
  "MY_API_URL": "dev.whateverMyServerIs.com"
}
  1. EnvironmentEnforcer will make sure that all of your promotions have the expected variables and that your promotions are without fear! :)

Other Examples

For other examples, please check out the __fixtures__/successCases folder in this repo for our integration tests that show working examples.

Configuration

  • environmentsFolderPathRelativeToPackageJSON
    • meaning: this is the folder where you would want to put the files that contain the values of your environment variables
    • default value: "./environments"
    • allowed values: a path string that is relative to package
  • stageNames
    • meaning: this is the array of strings that define the names of the possible NODE_ENV values that this macro will look for
    • default values: ['development', 'qa', 'staging', 'production', 'test']
    • allowed values: an array of any strings that you expect to be setting NODE_ENV to during your applications CICD process.

Setting This Configuration via file

This macro works a lot better if you DO NOT try to configure it. The defaults are industry standard. But we allow you to change the configuration if you wish. This is handled by the standard babel macro PLUGIN config file process which is described below:

  1. Place a file called babel-plugin-macros.config.js next to the file that uses the environmentEnforcer macro.
  2. The content of this file should look like this:
module.exports = {
  environmentEnforcer: {
    environmentsFolderPathRelativeToPackageJSON: 'mySpecialFolder/environments',
  },
};

Contributions

Contributions welcome as long as they come with strong automated test coverage and use conventional commit messages.

environment-enforcer's People

Contributors

dependabot[bot] avatar dgreene1 avatar

Watchers

 avatar  avatar

Forkers

mellis481

environment-enforcer's Issues

Make limitations known and clear via Errors (to enhance discoverability)

No one likes unhandled errors, but if we return errors that act as documentation... then we can be clear about this library’s current limitations and/or design goals. For instance...

Limitations:

  • don’t support the use of aliases (ie the type keyword)
  • Don’t support extending interfaces

Design Goals (to list a few that might be controversial and need to explaining):

  • to not allow the environment variables object just to become an object with any string and any value. That would make this library as useful as process.env itself.

typeParameters is not coming through

After integrating environment-enforcer into my repo, I'm seeing typeParameters is not coming through here resulting in the following error:

This macro requires you to explicitly pass the type parameter for the interface that defines the contract for which each environment variable file must adhere to. However, we did not find a type parameter for this node: {"type":"CallExpression","callee":{"type":"Identifier","name":"cov_78iegp7dg"},"arguments":[]}"

As you can see, p.node (logged above) has no typeParameters prop.

In the unit tests in this repo, I see that p.node is:

 {
        type: 'CallExpression',
        start: 136,
        end: 173,
        loc: SourceLocation {
          start: Position { line: 7, column: 23 },
          end: Position { line: 7, column: 60 },
          filename: undefined,
          identifierName: undefined
        },
        range: undefined,
        leadingComments: undefined,
        trailingComments: undefined,
        innerComments: undefined,
        extra: undefined,
        callee: Node {
          type: 'MemberExpression',
          start: 136,
          end: 161,
          loc: SourceLocation {
            start: [Position],
            end: [Position],
            filename: undefined,
            identifierName: undefined
          },
          range: undefined,
          leadingComments: undefined,
          trailingComments: undefined,
          innerComments: undefined,
          extra: undefined,
          object: Node {
            type: 'Identifier',
            start: 136,
            end: 155,
            loc: [SourceLocation],
            range: undefined,
            leadingComments: undefined,
            trailingComments: undefined,
            innerComments: undefined,
            extra: undefined,
            name: 'EnvironmentEnforcer'
          },
          computed: false,
          property: Node {
            type: 'Identifier',
            start: 156,
            end: 161,
            loc: [SourceLocation],
            range: undefined,
            leadingComments: undefined,
            trailingComments: undefined,
            innerComments: undefined,
            extra: undefined,
            name: 'parse'
          }
        },
        arguments: [],
        typeParameters: Node {
          type: 'TSTypeParameterInstantiation',
          start: 161,
          end: 171,
          loc: SourceLocation {
            start: [Position],
            end: [Position],
            filename: undefined,
            identifierName: undefined
          },
          range: undefined,
          leadingComments: undefined,
          trailingComments: undefined,
          innerComments: undefined,
          extra: undefined,
          params: [ [Node] ]
        }
      }

I'm unclear why typeParameters are not being passed through successfully.

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.