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 |
|
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 |
|
29 | # Table of contents
|
30 |
|
31 |
|
32 |
|
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 |
|
45 |
|
46 |
|
47 |
|
48 | <br>
|
49 |
|
50 |
|
51 |
|
52 | # Example without any config
|
53 |
|
54 | Run
|
55 | ```
|
56 | node ./index.js --env=prod
|
57 | ```
|
58 |
|
59 | prod.entor.json
|
60 | ```json
|
61 | {
|
62 | "db_url": "prod://url",
|
63 | "username": "prod"
|
64 | }
|
65 | ```
|
66 |
|
67 | index.js
|
68 | ```js
|
69 | require("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 | "db_url": "prod://url",
|
78 | "user": "prod"
|
79 | };
|
80 | ```
|
81 |
|
82 |
|
83 | <br>
|
84 |
|
85 |
|
86 |
|
87 | **❗ Note**
|
88 | Each value in `process.env` is converted to `string` by Node.
|
89 |
|
90 |
|
91 |
|
92 | <br>
|
93 |
|
94 |
|
95 |
|
96 | # Config
|
97 |
|
98 | ```js
|
99 | const 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 `<env>.entor.json`. Default `./`.
|
114 | <br>
|
115 |
|
116 | - **sharedEnvFilePath** `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 `<env>.entor.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 `<env>.entor.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 precedence:
|
140 |
|
141 | 1. `sharedEnvFilePath`
|
142 | 2. `env`
|
143 | 3. `override`
|
144 |
|
145 | `sharedEnvFilePath` will overwriteen by `env`.
|
146 | `env`will be overwritten by `override`.
|
147 |
|
148 |
|
149 |
|
150 | <br>
|
151 |
|
152 |
|
153 |
|
154 | # Examples
|
155 |
|
156 | ## Custom arguments
|
157 |
|
158 | Run
|
159 | ```
|
160 | node ./index.js --myCustomEnv=local
|
161 | ```
|
162 |
|
163 | index.js
|
164 | ```js
|
165 | require("entor")({
|
166 | getEnv: args => args.myCustomEnv,
|
167 | });
|
168 | ```
|
169 |
|
170 |
|
171 |
|
172 | ## Override
|
173 |
|
174 | Run
|
175 | ```
|
176 | node ./index.js --env=prod
|
177 | ```
|
178 |
|
179 | prod.entor.json
|
180 | ```json
|
181 | {
|
182 | "db_url": "prod://url",
|
183 | "username": "prod"
|
184 | }
|
185 | ```
|
186 |
|
187 | index.js
|
188 | ```js
|
189 | require("entor")({
|
190 | override: {
|
191 | db_url: "override://url"
|
192 | }
|
193 | });
|
194 | ```
|
195 |
|
196 | ✅ Entor will load `prod.entor.json` into process.env and apply the override.
|
197 |
|
198 | ```js
|
199 | process.env = {
|
200 | db_url: "override://url",
|
201 | username: "prod"
|
202 | };
|
203 | ```
|
204 |
|
205 |
|
206 |
|
207 | ## Shared env
|
208 |
|
209 | Run
|
210 | ```
|
211 | node ./index.js --env=prod
|
212 | ```
|
213 |
|
214 | shared.entor.json
|
215 | ```json
|
216 | {
|
217 | "db_url": "shared://url",
|
218 | }
|
219 | ```
|
220 |
|
221 | prod.entor.json
|
222 | ```json
|
223 | {
|
224 | "username": "prod"
|
225 | }
|
226 | ```
|
227 |
|
228 | index.js
|
229 | ```js
|
230 | require("entor")({
|
231 | sharedEnvFilePath: "C:/folder/project/shared.entor.json",
|
232 | });
|
233 | ```
|
234 |
|
235 | ✅ Entor will merge `shared.entor.json` with `prod.entor.json` and write into process.env.
|
236 |
|
237 | ```js
|
238 | process.env = {
|
239 | db_url: "shared://url",
|
240 | username: "prod"
|
241 | };
|
242 | ```
|
243 |
|
244 |
|
245 |
|
246 | <br>
|
247 |
|
248 |
|
249 |
|
250 | # <a name='table-of-contents'></a>[Go to top](#table-of-contents)
|
251 |
|
252 |
|