UNPKG

3.96 kBMarkdownView Raw
1
2<div style="text-align:center">
3 <font size="6">🌌</font>
4 <h1> entor </h1>
5</div>
6
7
8[![entor package size](https://packagephobia.com/badge?p=entor)](https://packagephobia.com/result?p=entor)
9<!-- [![entor package size minzipped](https://badgen.net/bundlephobia/minzip/entor)](https://badgen.net/bundlephobia/minzip/entor) [![entor dependency count](https://badgen.net/bundlephobia/dependency-count/entor)](https://badgen.net/bundlephobia/dependency-count/entor) -->
10
11
12**entor** loads environment variables from a JSON file into process.env.
13
14
15- 🚀 Easy JSON configuration
16- ⚪️ Zero dependencies
17- 💪 Flexibility
18
19
20
21<br>
22
23
24
25<!-- TOC ignore:true -->
26# Table of contents
27
28
29<!-- TOC -->
30
31- [Table of contents](#table-of-contents)
32- [Example without any config](#example-without-any-config)
33 - [Note ❗](#note-)
34- [Config](#config)
35- [Override precedence:](#override-precedence)
36- [Examples](#examples)
37 - [Custom arguments](#custom-arguments)
38 - [Override](#override)
39 - [Shared env](#shared-env)
40- [<a name='table-of-contents'></a>Go to top](#go-to-top)
41
42<!-- /TOC -->
43
44
45
46<br>
47
48
49
50# Example without any config
51
52Run
53```
54node ./index.js --entor=prod
55```
56
57prod.entor.json
58```json
59{
60 "db_url": "prod://url",
61 "username": "prod"
62}
63```
64
65index.js
66```js
67require("entor")();
68// OR
69// const environment = require("entor")({ addToProcessEnv: false });
70```
71
72✅ Entor will load `local.entor.json` into process.env.
73```js
74process.env = {
75 "db_url": "prod://url",
76 "user": "prod"
77};
78```
79
80
81<br>
82
83
84
85### Note ❗
86Each value in `process.env` is converted to `string` by Node.
87
88
89
90<br>
91
92
93
94# Config
95
96```js
97require("entor")({config});
98```
99
100<br>
101
102- **getEnvType** `function`:
103 - Function that receives as argument the object with process arguments (`--key=value``{key: value}`) that must return a string containing the `envType`.
104 - Default: `args => args.entor`.
105- **envType** `string`: Defines the environment type. This will take precedence over `getEnvType`.
106- **path** `string`: Defines the path where will look for the file `<envType>.entor.json`. Default `./`.
107- **sharedEnvFilePath** `string`: Defines the file path where a `.json` will be loaded.
108- **override** `object`: object that will be merged with the content of `<envType>.entor.json`.
109- **warningLevel** `"none" | "message" | "throw"`:
110 - `"none"` will **ignore all** non-critical errors.
111 - `"message"` will print all errors but will **never throw**.
112 - `"throw"` will print all errors, **throw on critical errors**.
113
114
115
116<br>
117
118
119
120# Override precedence:
121
1221. `sharedEnvFilePath`
1232. `envType`
1243. `override`
125
126`sharedEnvFilePath` will overwriteen by `envType`.
127`envType`will be overwritten by `override`.
128
129
130
131<br>
132
133
134
135# Examples
136
137## Custom arguments
138
139Run
140```
141node ./index.js --myEnv=local
142```
143
144index.js
145```js
146require("entor")({
147 getEnvType: args => args.myEnv,
148});
149```
150
151
152
153## Override
154
155Run
156```
157node ./index.js --entor=prod
158```
159
160prod.entor.json
161```json
162{
163 "db_url": "prod://url",
164 "username": "prod"
165}
166```
167
168index.js
169```js
170require("entor")({
171 override: {
172 db_url: "override://url"
173 }
174});
175```
176
177✅ Entor will load `prod.entor.json` into process.env and apply the override.
178
179```js
180process.env = {
181 db_url: "override://url",
182 username: "prod"
183};
184```
185
186
187
188## Shared env
189
190Run
191```
192node ./index.js --entor=prod
193```
194
195shared.entor.json
196```json
197{
198 "db_url": "shared://url",
199}
200```
201
202prod.entor.json
203```json
204{
205 "username": "prod"
206}
207```
208
209index.js
210```js
211require("entor")({
212 sharedEnvFilePath: "C:/folder/project/shared.entor.json",
213});
214```
215
216✅ Entor will merge `shared.entor.json` with `prod.entor.json` and write into process.env.
217
218```js
219process.env = {
220 db_url: "shared://url",
221 username: "prod"
222};
223```
224
225
226
227<br>
228
229
230
231# <a name='table-of-contents'></a>[Go to top](#table-of-contents)
232
233