@apparts/config
v1.3.0
Published
A config-loading framework
Downloads
8
Readme
#+TITLE: @apparts/config #+DATE: [2019-02-07 Thu] #+AUTHOR: Philipp Uhl
- Usage
This package can read configuration from environment variables, or, if not found in, from the folder =config= in the root-directory of the project.
** As environment variable
Storage in an environment variable can be done in two ways:
- as a string that is parsable by ~JSON.parse~
- as a Base-64 encoded string that decodes to a ~JSON.parse~-parsable string
** From a folder when running in a Node environment
To require a configuration that is stored in one of the files
- =config/my-color.json=
- =config/my-color.js= or the environment variable =MY_COLOR= (note, a dash becomes an underscore and everything is in uppercase): #+BEGIN_SRC sh const Colors = require('apparts-config').get('my-color'); #+END_SRC
** Webpack for web environment
To require a configuration that is stored in the environment variable =MY_COLOR= (note, a dash becomes an underscore and everything is in uppercase): #+BEGIN_SRC sh const Colors = require('apparts-config').get('my-color'); #+END_SRC
In order to create environment variables use the following webpack (v
2.0) configuration options:
#+BEGIN_SRC js module.exports = env => { return { plugins: [ new webpack.DefinePlugin({ 'process.env': { 'MY_COLOR': JSON.stringify(JSON.stringify(require('./src/utils/colors.js'))) } }), ] } }; #+END_SRC
** React environment
In an react environment, you must call at the beginning of your application the following code. That ensures, that @apparts/config can access the environment variables.
#+BEGIN_SRC js import { setEnv } from "@apparts/config"; setEnv(process.env); #+END_SRC
Your config variables need to be stored in the =.env= file, with all config names prefixed with =REACT_APP_=.
To get the config =my-color= you must store =REACT_APP_MY_COLOR= in your =.env= file. You can store the values as raw string or base64 encoded.