UNPKG

4.38 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 precedence:](#override-precedence)
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
54Run
55```
56node ./index.js --env=prod
57```
58
59entor.prod.json
60```json
61{
62 "db_url": "prod://url",
63 "username": "prod"
64}
65```
66
67index.js
68```js
69require("entor")();
70```
71
72✅ Entor will load `entor.prod.json` into process.env.
73```js
74// process.env
75{
76 "db_url": "prod://url",
77 "user": "prod"
78}
79```
80
81
82<br>
83
84
85
86**❗ Note**
87Each value in `process.env` is converted to `string` by Node.
88
89
90
91<br>
92
93
94
95# Config
96
97```js
98const env = require("entor")({config});
99// ↑ entor will always return env object
100```
101
102<br>
103
104- **getEnv** `function`:
105 - Function that receives as argument the object with process arguments (`--key=value``{key: value}`) that must return a string containing the `env`.
106 - Default: `args => args.env`.
107<br>
108
109- **env** `string`: Defines the environment type. This will take precedence over `getEnv`.
110<br>
111
112- **path** `string`: Defines the path where will look for the file `entor.<env>.json`. Default `./`.
113<br>
114
115- **sharedEnvPath** `string`: Defines the file path where a `.json` will be loaded.
116<br>
117
118- **override** `object`: object that will be merged with the content of `entor.<env>.json`.
119<br>
120
121- **warningLevel** `"none" | "message" | "throw"`:
122 - `"none"` will **ignore all** non-critical errors.
123 - `"message"` will print all errors but will **never throws**.
124 - `"throw"` will print all errors, **throws on critical errors**.
125<br>
126
127- **addToProcessEnv** `boolean`: Default `true`. If `true` adds the `entor.<env>.json` content to the `process.env` object.
128<br>
129
130- **syncExamples** `boolean`: Default `false`. If `true` syncs the `entor.<env>.json` file with the `entorExample.<env>.json` file.
131
132
133
134<br>
135
136
137
138# Override precedence:
139
1401. `sharedEnvPath`
1412. `env`
1423. `override`
143
144`sharedEnvPath` will overwriteen by `env`.
145`env`will be overwritten by `override`.
146
147
148
149<br>
150
151
152
153# Examples
154
155## Custom arguments
156
157Run
158```
159node ./index.js --myCustomEnv=local
160```
161
162index.js
163```js
164require("entor")({
165 getEnv: args => args.myCustomEnv,
166});
167```
168
169
170
171## Override
172
173Run
174```
175node ./index.js --env=prod
176```
177
178entor.prod.json
179```json
180{
181 "db_url": "prod://url",
182 "username": "prod"
183}
184```
185
186index.js
187```js
188require("entor")({
189 override: {
190 db_url: "override://url"
191 }
192});
193```
194
195✅ Entor will load `entor.prod.json` into process.env and apply the override.
196
197```js
198// process.env
199{
200 db_url: "override://url",
201 username: "prod"
202}
203```
204
205
206
207## Shared env
208
209Run
210```
211node ./index.js --env=prod
212```
213
214entor.prod.json (located at the parent folder)
215```json
216{
217 "db_url": "shared://url",
218}
219```
220
221entor.prod.json (located at project folder)
222```json
223{
224 "username": "prod"
225}
226```
227
228index.js
229```js
230require("entor")({
231 sharedEnvPath: "C:/parentFolder/",
232});
233```
234
235✅ Entor will merge `entor.prod.json` (shared) with `prod.entor.json` (project) and write into process.env.
236
237```js
238// process.env
239{
240 db_url: "shared://url",
241 username: "prod"
242}
243```
244
245
246
247<br>
248
249
250
251# <a name='table-of-contents'></a>[Go to top](#table-of-contents)
252
253