Skip to content

Commit 25ce20d

Browse files
committed
+ test folder -> tests
+ babel build rework + preload result kept in global
1 parent 06f7fcd commit 25ce20d

34 files changed

+436
-22
lines changed

README.md

+89
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,95 @@ console.log(process.env.EXPONENTIAL) // (string) 'false'
138138
console.log(process.env.NUMBER) // (string) '100'
139139
```
140140

141+
## Preload
142+
143+
You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module)
144+
to preload dotenv & dotenv-conversion (and even dotenv-expand).
145+
By doing this, you do not need to require and load dotenv or dotenv-conversion (or dotenv-expand)
146+
in your application code.
147+
This is the preferred approach when using `import` instead of `require`.
148+
149+
```bash
150+
# dotenv + dotenv-conversion
151+
$ node -r dotenv-conversion/config your_script.js
152+
153+
# dotenv + dotenv-expand + dotenv-conversion
154+
$ node -r dotenv-conversion/config-expand your_script.js
155+
```
156+
157+
The configuration options below are supported as command line arguments
158+
in the format `dotenv_config_<option>=value`.
159+
160+
```bash
161+
# dotenv + dotenv-conversion
162+
$ node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
163+
164+
# dotenv + dotenv-expand + dotenv-conversion
165+
$ node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
166+
```
167+
168+
Additionally, you can use environment variables to set configuration options.
169+
Command line arguments will precede these.
170+
171+
```bash
172+
# dotenv + dotenv-conversion
173+
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv-conversion/config your_script.js
174+
175+
# dotenv + dotenv-expand + dotenv-conversion
176+
$ DOTENV_CONFIG_<OPTION>=value node -r dotenv-conversion/config your_script.js
177+
```
178+
179+
```bash
180+
# dotenv + dotenv-conversion
181+
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/.env
182+
183+
# dotenv + dotenv-expand + dotenv-conversion
184+
$ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv-conversion/config your_script.js dotenv_config_path=/custom/path/to/.env
185+
```
186+
187+
After preload, you can retrieve converted variables via `global.dotenvConversion.parsed`:
188+
189+
```dotenv
190+
# .env file
191+
DEBUG_LEVEL=0
192+
DEBUG=boolean:$DEBUG_LEVEL
193+
194+
EXPONENTIAL=2
195+
NUMBER=1e$EXPONENTIAL
196+
```
197+
198+
```javascript
199+
// index.js file
200+
const parsedEnv = global.dotenvConversion.parsed
201+
console.log(parsedEnv.DEBUG_LEVEL) // (number) 0
202+
console.log(parsedEnv.DEBUG) // (boolean) false
203+
console.log(parsedEnv.EXPONENTIAL) // (boolean) 2
204+
console.log(parsedEnv.NUMBER) // (boolean) 100
205+
console.log(process.env.DEBUG_LEVEL) // (string) '0'
206+
console.log(process.env.DEBUG) // (string) 'false'
207+
console.log(process.env.EXPONENTIAL) // (string) 'false'
208+
console.log(process.env.NUMBER) // (string) '100'
209+
```
210+
211+
```bash
212+
# dotenv + dotenv-expand + dotenv-conversion
213+
$ node -r dotenv-conversion/config-expand index.js
214+
```
215+
216+
Console output:
217+
218+
```
219+
0
220+
false
221+
2
222+
100
223+
0
224+
false
225+
2
226+
100
227+
```
228+
229+
141230
## Features
142231

143232
### Auto-Conversion

babel.config.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
],
55
"plugins": [
66
"add-module-exports"
7-
],
8-
"comments": false,
9-
"sourceMaps": true,
10-
"minified": true
7+
]
118
}

config-expand.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
),
1111
)
1212

13-
return dotenvConversion(dotenvExpand(env))
13+
return global.dotenvConversion = dotenvConversion(dotenvExpand(env))
1414
})()

config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
),
1010
)
1111

12-
return dotenvConversion(env)
12+
return global.dotenvConversion = dotenvConversion(env)
1313
})()

0 commit comments

Comments
 (0)