UNPKG

4.47 kBMarkdownView Raw
1
2<div style="text-align:center">
3 <h1> entor </h1>
4 <img src="https://i.gyazo.com/0bdbfe268d70a6fc916ef4faa2e6b68d.png">
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<br>
12
13**entor** loads environment variables from a JSON file into process.env.
14
15
16- 🚀 Easy JSON configuration
17- ⚪️ Zero dependencies
18- 💪 Flexibility
19- 🎎 Shared environment between all your projects
20- 🔨 Generates examples automatically
21
22
23
24<br>
25
26
27
28<!-- TOC ignore:true -->
29# Table of contents
30
31
32<!-- TOC -->
33
34- [Table of contents](#table-of-contents)
35- [Example without any config](#example-without-any-config)
36- [Config](#config)
37- [Override priority](#override-priority)
38- [Examples](#examples)
39 - [Custom arguments](#custom-arguments)
40 - [Override](#override)
41 - [Shared env](#shared-env)
42- [<a name='table-of-contents'></a>Go to top](#go-to-top)
43
44<!-- /TOC -->
45
46
47
48<br>
49
50
51
52# Example without any config
53
54
55```json
56// entor.prod.json
57{
58 "db_url": "prod://url",
59 "username": "prod"
60}
61```
62
63```js
64// index.js
65require("entor")();
66```
67
68🏃‍♂️ Run
69```
70node ./index.js --env=prod
71```
72
73✅ Entor will load `entor.prod.json` into process.env.
74```js
75// process.env
76{
77 "db_url": "prod://url",
78 "user": "prod"
79}
80```
81
82
83<br>
84
85
86
87**❗ Note**
88Each value in `process.env` is converted to `string` by Node.
89
90
91
92<br>
93
94
95
96# Config
97
98```js
99const env = require("entor")({config});
100// ↑ entor will always return env object
101```
102
103<br>
104
105- **getEnv** `function`:
106 - Function that receives as argument the object with process arguments (`--key=value``{key: value}`) that must return a string containing the `env`.
107 - Default: `args => args.env`.
108<br>
109
110- **env** `string`: Defines the environment type. This will take precedence over `getEnv`.
111<br>
112
113- **path** `string`: Defines the path where will look for the file `entor.<env>.json`. Default `./`.
114<br>
115
116- **sharedEnvPath** `string`: Defines the file path where a `.json` will be loaded.
117<br>
118
119- **override** `object`: object that will be merged with the content of `entor.<env>.json`.
120<br>
121
122- **warningLevel** `"none" | "message" | "throw"`:
123 - `"none"` will **ignore all** non-critical errors.
124 - `"message"` will print all errors but will **never throws**.
125 - `"throw"` will print all errors, **throws on critical errors**.
126<br>
127
128- **addToProcessEnv** `boolean`: Default `true`. If `true` adds the `entor.<env>.json` content to the `process.env` object.
129<br>
130
131- **syncExamples** `boolean`: Default `false`. If `true` syncs the `entor.<env>.json` file with the `entorExample.<env>.json` file.
132
133
134
135<br>
136
137
138
139# Override priority
140
1411. `override` will override ↓
1422. `env` will override ↓
1433. `sharedEnvPath` (This is the first file that will be loaded)
144
145
146
147<br>
148
149
150
151# Examples
152
153## Custom arguments
154
155```js
156// index.js
157require("entor")({
158 getEnv: args => args.myCustomEnv,
159});
160```
161
162🏃‍♂️ Run
163```
164node ./index.js --myCustomEnv=local
165```
166
167
168
169## Override
170
171
172```json
173// entor.prod.json
174{
175 "db_url": "prod://url",
176 "username": "prod"
177}
178```
179
180```js
181// index.js
182require("entor")({
183 override: {
184 db_url: "override://url"
185 }
186});
187```
188
189🏃‍♂️ Run
190```
191node ./index.js --env=prod
192```
193
194
195✅ Entor will load `entor.prod.json` into process.env and apply the override.
196
197```js
198// process.env values:
199{
200 db_url: "override://url",
201 username: "prod"
202}
203```
204
205
206
207## Shared env
208
209```json
210// entor.prod.json (located at the parent folder)
211{
212 "db_url": "shared://url",
213}
214```
215
216```json
217// entor.prod.json (located at project folder)
218{
219 "username": "prod"
220}
221```
222
223```js
224// index.js
225require("entor")({
226 sharedEnvPath: "C:/parentFolder/",
227});
228```
229
230🏃‍♂️ Run
231```
232node ./index.js --env=prod
233```
234
235
236✅ Entor will merge `entor.prod.json` (shared) with `prod.entor.json` (project) and write into process.env.
237
238```js
239// process.env values:
240{
241 db_url: "shared://url",
242 username: "prod"
243}
244```
245
246
247
248<br>
249
250
251
252# <a name='table-of-contents'></a>[Go to top](#table-of-contents)
253
254