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