"When you want your code to work in every environment, call on the Environment Enforcer."
- run
npm install environment-enforcer.macro
- Create a single file that will provide your code access to your environment files. Let's call it
wrappedEnvVars.ts
- 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>();
- create a folder called
environments
(or see Configuration below for other options) and place it at the level ofpackage.json
- 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/
- 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"
}
- EnvironmentEnforcer will make sure that all of your promotions have the expected variables and that your promotions are without fear! :)
For other examples, please check out the __fixtures__/successCases
folder in this repo for our integration tests that show working examples.
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.
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:
- Place a file called
babel-plugin-macros.config.js
next to the file that uses the environmentEnforcer macro. - The content of this file should look like this:
module.exports = {
environmentEnforcer: {
environmentsFolderPathRelativeToPackageJSON: 'mySpecialFolder/environments',
},
};
Contributions welcome as long as they come with strong automated test coverage and use conventional commit messages.