@karmaniverous/get-dotenv 3.0.5 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -121
- package/bin/getdotenv/index.js +3 -3
- package/dist/default/lib/getDotenv.js +24 -18
- package/dist/default/lib/getDotenvCli.js +191 -0
- package/dist/default/lib/index.js +5 -5
- package/dist/default/lib/options.js +51 -0
- package/dist/default/lib/readDotenv.js +2 -2
- package/getdotenv.config.json +24 -0
- package/lib/getDotenv.js +28 -16
- package/lib/getDotenvCli.js +450 -0
- package/lib/index.js +1 -1
- package/lib/options.js +79 -0
- package/lib/readDotenv.js +2 -2
- package/package.json +5 -4
- package/dist/default/lib/dotenvDefaults.js +0 -20
- package/dist/default/lib/getCli.js +0 -202
- package/lib/dotenvDefaults.js +0 -13
- package/lib/getCli.js +0 -360
package/README.md
CHANGED
|
@@ -6,14 +6,18 @@ Load environment variables with a cascade of environment-aware dotenv files. You
|
|
|
6
6
|
- Load variables for a specific environment or none.
|
|
7
7
|
- Define dynamic variables progressively in terms of other variables and other logic.
|
|
8
8
|
- Exclude public, private, global, environment-specific, or dynamic variables.
|
|
9
|
+
- Specify explicit variables to include.
|
|
9
10
|
- Extract the resulting variables to an object, `process.env`, a dotenv file, or a logger, in any combination.
|
|
11
|
+
- Execute a shell command within the resulting environment. You can even nest additional `getdotenv` calls!
|
|
10
12
|
- Specify the directories containing your dotenv files.
|
|
11
13
|
- Specify the filename token that identifies dotenv files (e.g. '.env').
|
|
12
14
|
- Specify the filename extension that identifies private variables (e.g. 'local').
|
|
15
|
+
- Set defaults for all options in a [`getdotenv.config.json`](./getdotenv.config.json) file in your project root directory.
|
|
16
|
+
- Generate a custom `getdotenv`-based CLI for use in your own projects.
|
|
13
17
|
|
|
14
18
|
`getdotenv` relies on the excellent [`dotenv`](https://www.npmjs.com/package/dotenv) parser and uses [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) for recursive variable expansion.
|
|
15
19
|
|
|
16
|
-
The command-line version populates `process.env` from your dotenv files (you can also specify values inline) and can then execute a shell command within that context.
|
|
20
|
+
The command-line version populates `process.env` from your dotenv files (you can also specify values inline) and can then execute a shell command within that context. Any child `getdotenv` instances will inherit as defaults the parent shell's environment and optionally its `getdotenv` settings.
|
|
17
21
|
|
|
18
22
|
You can always use `getdotenv` directly on the command line, but its REAL power comes into play when you use it as the foundation of your own CLI. This lets you set defaults globally and configure pre- and post-hooks that mutate your `getdotenv` context and do useful things like grab an AWS session from your dev environment and add it to the command execution context.
|
|
19
23
|
|
|
@@ -63,7 +67,6 @@ Since keys will be evaluated progressively, each successive key function will ha
|
|
|
63
67
|
|
|
64
68
|
Implementation always runs a little behind documentation. These topics & improvements are coming soon:
|
|
65
69
|
|
|
66
|
-
- Rationalize the package's JSDOC to improve the API documentation below.
|
|
67
70
|
- An example of dotenv-based environment config.
|
|
68
71
|
- Integrating `getdotenv` into your npm scripts.
|
|
69
72
|
- Creating a `getdotenv`-based CLI.
|
|
@@ -71,58 +74,56 @@ Implementation always runs a little behind documentation. These topics & improve
|
|
|
71
74
|
|
|
72
75
|
# Command Line Interface
|
|
73
76
|
|
|
74
|
-
Note that the defaults below can be changed in your own environment by deriving your base CLI using the `
|
|
77
|
+
Note that the defaults below can be changed in your own environment by deriving your base CLI using the `getDotenvCli` function or placing a `getdotenv.options.json` file in your project root directory.
|
|
75
78
|
|
|
76
79
|
```text
|
|
77
80
|
Usage: getdotenv [options] [command]
|
|
78
81
|
|
|
79
|
-
Base CLI.
|
|
82
|
+
Base CLI.
|
|
80
83
|
|
|
81
84
|
Options:
|
|
82
|
-
-e, --env <string>
|
|
83
|
-
|
|
84
|
-
-
|
|
85
|
-
--
|
|
86
|
-
-
|
|
87
|
-
--
|
|
88
|
-
--
|
|
89
|
-
-
|
|
90
|
-
-
|
|
91
|
-
-
|
|
92
|
-
-
|
|
93
|
-
-
|
|
94
|
-
-
|
|
95
|
-
-
|
|
96
|
-
-
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
-
|
|
100
|
-
-
|
|
101
|
-
-
|
|
102
|
-
-
|
|
103
|
-
-
|
|
104
|
-
-
|
|
105
|
-
-
|
|
106
|
-
--
|
|
107
|
-
--
|
|
108
|
-
-
|
|
109
|
-
-
|
|
85
|
+
-e, --env <string> target environment
|
|
86
|
+
-v, --vars <string> dotenv-expanded delimited key-value pairs: KEY1=VAL1 KEY2=VAL2 (default: "")
|
|
87
|
+
-c, --command <string> dotenv-expanded shell command string
|
|
88
|
+
-o, --output-path <string> consolidated output file, follows dotenv-expand rules using loaded env vars
|
|
89
|
+
-p, --load-process load variables to process.env ON (default)
|
|
90
|
+
-P, --load-process-off load variables to process.env OFF
|
|
91
|
+
-a, --exclude-all exclude all dotenv variables from loading ON
|
|
92
|
+
-A, --exclude-all-off exclude all dotenv variables from loading OFF (default)
|
|
93
|
+
-z, --exclude-dynamic exclude dynamic dotenv variables from loading ON
|
|
94
|
+
-Z, --exclude-dynamic-off exclude dynamic dotenv variables from loading OFF (default)
|
|
95
|
+
-n, --exclude-env exclude environment-specific dotenv variables from loading
|
|
96
|
+
-N, --exclude-env-off exclude environment-specific dotenv variables from loading OFF (default)
|
|
97
|
+
-g, --exclude-global exclude global dotenv variables from loading ON
|
|
98
|
+
-G, --exclude-global-off exclude global dotenv variables from loading OFF (default)
|
|
99
|
+
-r, --exclude-private exclude private dotenv variables from loading ON
|
|
100
|
+
-R, --exclude-private-off exclude private dotenv variables from loading OFF (default)
|
|
101
|
+
-u, --exclude-public exclude public dotenv variables from loading ON
|
|
102
|
+
-U, --exclude-public-off exclude public dotenv variables from loading OFF (default)
|
|
103
|
+
-l, --log console log loaded variables ON
|
|
104
|
+
-L, --log-off console log loaded variables OFF (default)
|
|
105
|
+
-d, --debug debug mode ON
|
|
106
|
+
-D, --debug-off debug mode OFF (default)
|
|
107
|
+
--default-env <string> default target environment (default: "dev")
|
|
108
|
+
--dotenv-token <string> dotenv-expanded token indicating a dotenv file (default: ".env")
|
|
109
|
+
--dynamic-path <string> dynamic variables path (default: "./env/dynamic.js")
|
|
110
|
+
--paths <string> dotenv-expanded delimited list of paths to dotenv directory (default: "./ ./env")
|
|
111
|
+
--paths-delimiter <string> paths delimiter string (default: " ")
|
|
112
|
+
--paths-delimiter-pattern <string> paths delimiter regex pattern (default: "\\s+")
|
|
113
|
+
--private-token <string> dotenv-expanded token indicating private variables (default: "local")
|
|
114
|
+
--vars-delimiter <string> vars delimiter string (default: " ")
|
|
115
|
+
--vars-delimiter-pattern <string> vars delimiter regex pattern (default: "\\s+")
|
|
116
|
+
--vars-assignor <string> vars assignment operator string (default: "=")
|
|
117
|
+
--vars-assignor-pattern <string> vars assignment operator regex pattern (default: "=")
|
|
118
|
+
-h, --help display help for command
|
|
110
119
|
|
|
111
120
|
Commands:
|
|
112
|
-
cmd
|
|
113
|
-
help [command]
|
|
121
|
+
cmd execute shell command string (default command)
|
|
122
|
+
help [command] display help for command
|
|
114
123
|
```
|
|
115
124
|
|
|
116
125
|
# API Documentation
|
|
117
126
|
|
|
118
|
-
## Constants
|
|
119
|
-
|
|
120
|
-
<dl>
|
|
121
|
-
<dt><a href="#getCli">getCli</a> ⇒ <code>object</code></dt>
|
|
122
|
-
<dd><p>Generate a CLI for get-dotenv.</p>
|
|
123
|
-
</dd>
|
|
124
|
-
</dl>
|
|
125
|
-
|
|
126
127
|
## Functions
|
|
127
128
|
|
|
128
129
|
<dl>
|
|
@@ -132,40 +133,25 @@ Commands:
|
|
|
132
133
|
<dt><a href="#getDotenvSync">getDotenvSync([options])</a> ⇒ <code>Object</code></dt>
|
|
133
134
|
<dd><p>Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]</p>
|
|
134
135
|
</dd>
|
|
136
|
+
<dt><a href="#getDotenvCli">getDotenvCli([options])</a> ⇒ <code>object</code></dt>
|
|
137
|
+
<dd><p>Generate a CLI for get-dotenv.</p>
|
|
138
|
+
</dd>
|
|
135
139
|
</dl>
|
|
136
140
|
|
|
137
141
|
## Typedefs
|
|
138
142
|
|
|
139
143
|
<dl>
|
|
140
|
-
<dt><a href="#
|
|
141
|
-
<dd><p>
|
|
144
|
+
<dt><a href="#GetDotenvOptions">GetDotenvOptions</a> : <code>Object</code></dt>
|
|
145
|
+
<dd><p>get-dotenv options type</p>
|
|
142
146
|
</dd>
|
|
143
|
-
<dt><a href="#GetDotenvPreHookCallback">GetDotenvPreHookCallback</a> ⇒ <code
|
|
147
|
+
<dt><a href="#GetDotenvPreHookCallback">GetDotenvPreHookCallback</a> ⇒ <code>GetDotenvCliOptions</code></dt>
|
|
144
148
|
<dd><p>GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.</p>
|
|
145
149
|
</dd>
|
|
146
150
|
<dt><a href="#GetDotenvPostHookCallback">GetDotenvPostHookCallback</a> : <code>function</code></dt>
|
|
147
151
|
<dd><p>GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.</p>
|
|
148
152
|
</dd>
|
|
149
|
-
<dt><a href="#GetDotenvCliConfig">GetDotenvCliConfig</a> : <code>Object</code></dt>
|
|
150
|
-
<dd><p>GetDotenv CLI Config type</p>
|
|
151
|
-
</dd>
|
|
152
|
-
<dt><a href="#OptionsType">OptionsType</a> : <code>Object</code></dt>
|
|
153
|
-
<dd><p>get-dotenv options type</p>
|
|
154
|
-
</dd>
|
|
155
153
|
</dl>
|
|
156
154
|
|
|
157
|
-
<a name="getCli"></a>
|
|
158
|
-
|
|
159
|
-
## getCli ⇒ <code>object</code>
|
|
160
|
-
Generate a CLI for get-dotenv.
|
|
161
|
-
|
|
162
|
-
**Kind**: global constant
|
|
163
|
-
**Returns**: <code>object</code> - The CLI command.
|
|
164
|
-
|
|
165
|
-
| Param | Type | Description |
|
|
166
|
-
| --- | --- | --- |
|
|
167
|
-
| [config] | [<code>GetDotenvCliConfig</code>](#GetDotenvCliConfig) | config object |
|
|
168
|
-
|
|
169
155
|
<a name="getDotenv"></a>
|
|
170
156
|
|
|
171
157
|
## getDotenv([options]) ⇒ <code>Promise.<object></code>
|
|
@@ -176,7 +162,7 @@ Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
|
|
|
176
162
|
|
|
177
163
|
| Param | Type | Description |
|
|
178
164
|
| --- | --- | --- |
|
|
179
|
-
| [options] | [<code>
|
|
165
|
+
| [options] | [<code>GetDotenvOptions</code>](#GetDotenvOptions) | options object |
|
|
180
166
|
|
|
181
167
|
<a name="getDotenvSync"></a>
|
|
182
168
|
|
|
@@ -188,21 +174,57 @@ Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
|
|
|
188
174
|
|
|
189
175
|
| Param | Type | Description |
|
|
190
176
|
| --- | --- | --- |
|
|
191
|
-
| [options] | [<code>
|
|
177
|
+
| [options] | [<code>GetDotenvOptions</code>](#GetDotenvOptions) | options object |
|
|
192
178
|
|
|
193
|
-
<a name="
|
|
179
|
+
<a name="getDotenvCli"></a>
|
|
194
180
|
|
|
195
|
-
##
|
|
196
|
-
|
|
181
|
+
## getDotenvCli([options]) ⇒ <code>object</code>
|
|
182
|
+
Generate a CLI for get-dotenv.
|
|
183
|
+
|
|
184
|
+
**Kind**: global function
|
|
185
|
+
**Returns**: <code>object</code> - The CLI command.
|
|
186
|
+
|
|
187
|
+
| Param | Type | Description |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| [options] | <code>object</code> | options object |
|
|
190
|
+
| [options.alias] | <code>string</code> | cli alias (used for cli help) |
|
|
191
|
+
| [options.command] | <code>string</code> | [dotenv-expanded](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env) shell command string |
|
|
192
|
+
| [options.debug] | <code>bool</code> | debug mode |
|
|
193
|
+
| [options.defaultEnv] | <code>string</code> | default target environment |
|
|
194
|
+
| [options.description] | <code>string</code> | cli description (used for cli help) |
|
|
195
|
+
| [options.dotenvToken] | <code>string</code> | [dotenv-expanded](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env) token indicating a dotenv file |
|
|
196
|
+
| [options.dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
|
|
197
|
+
| [options.excludeDynamic] | <code>bool</code> | exclude dynamic dotenv variables |
|
|
198
|
+
| [options.excludeEnv] | <code>bool</code> | exclude environment-specific dotenv variables |
|
|
199
|
+
| [options.excludeGlobal] | <code>bool</code> | exclude global dotenv variables |
|
|
200
|
+
| [options.excludePrivate] | <code>bool</code> | exclude private dotenv variables |
|
|
201
|
+
| [options.excludePublic] | <code>bool</code> | exclude public dotenv variables |
|
|
202
|
+
| [options.loadProcess] | <code>bool</code> | load variables to process.env |
|
|
203
|
+
| [options.log] | <code>bool</code> | log result to console |
|
|
204
|
+
| [options.logger] | <code>function</code> | logger function |
|
|
205
|
+
| [options.outputPath] | <code>string</code> | consolidated output file, [dotenv-expanded](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env) using loaded env vars |
|
|
206
|
+
| [options.paths] | <code>string</code> | [dotenv-expanded](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env) delimited list of input directory paths |
|
|
207
|
+
| [options.pathsDelimiter] | <code>string</code> | paths delimiter string |
|
|
208
|
+
| [options.pathsDelimiterPattern] | <code>string</code> | paths delimiter regex pattern |
|
|
209
|
+
| [config.preHook] | [<code>GetDotenvPreHookCallback</code>](#GetDotenvPreHookCallback) | transforms cli options & executes side effects |
|
|
210
|
+
| [options.privateToken] | <code>string</code> | [dotenv-expanded](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env) token indicating private variables |
|
|
211
|
+
| [config.postHook] | [<code>GetDotenvPostHookCallback</code>](#GetDotenvPostHookCallback) | executes side effects within getdotenv context |
|
|
212
|
+
| [options.vars] | <code>string</code> | [dotenv-expanded](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env) delimited list of explicit environment variable key-value pairs |
|
|
213
|
+
| [options.varsAssignor] | <code>string</code> | variable key-value assignor string |
|
|
214
|
+
| [options.varsAssignorPattern] | <code>string</code> | variable key-value assignor regex pattern |
|
|
215
|
+
| [options.varsDelimiter] | <code>string</code> | variable key-value pair delimiter string |
|
|
216
|
+
| [options.varsDelimiterPattern] | <code>string</code> | variable key-value pair delimiter regex pattern |
|
|
217
|
+
|
|
218
|
+
<a name="GetDotenvOptions"></a>
|
|
219
|
+
|
|
220
|
+
## GetDotenvOptions : <code>Object</code>
|
|
221
|
+
get-dotenv options type
|
|
197
222
|
|
|
198
223
|
**Kind**: global typedef
|
|
199
224
|
**Properties**
|
|
200
225
|
|
|
201
226
|
| Name | Type | Description |
|
|
202
227
|
| --- | --- | --- |
|
|
203
|
-
| [cliInvocation] | <code>string</code> | cli invocation string (used for cli help) |
|
|
204
|
-
| [debug] | <code>bool</code> | debug mode |
|
|
205
|
-
| [defaultEnv] | <code>string</code> | default target environment |
|
|
206
228
|
| [dotenvToken] | <code>string</code> | token indicating a dotenv file |
|
|
207
229
|
| [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
|
|
208
230
|
| [env] | <code>string</code> | target environment |
|
|
@@ -211,25 +233,25 @@ GetDotenv CLI Options type
|
|
|
211
233
|
| [excludeGlobal] | <code>bool</code> | exclude global & dynamic variables |
|
|
212
234
|
| [excludePrivate] | <code>bool</code> | exclude private variables |
|
|
213
235
|
| [excludePublic] | <code>bool</code> | exclude public variables |
|
|
214
|
-
| [
|
|
236
|
+
| [loadProcess] | <code>bool</code> | load dotenv to process.env |
|
|
237
|
+
| [log] | <code>bool</code> | log result to logger |
|
|
215
238
|
| [logger] | <code>function</code> | logger function |
|
|
216
239
|
| [outputPath] | <code>string</code> | if populated, writes consolidated .env file to this path (follows [dotenv-expand rules](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env)) |
|
|
217
|
-
| [paths] | <code>string
|
|
218
|
-
| [privateToken] | <code>string</code> | token indicating private variables
|
|
219
|
-
| [
|
|
220
|
-
| [suppressDotenv] | <code>bool</code> | suppress dotenv loading |
|
|
240
|
+
| [paths] | <code>Array.<string></code> | array of input directory paths |
|
|
241
|
+
| [privateToken] | <code>string</code> | token indicating private variables |
|
|
242
|
+
| [vars] | <code>object</code> | explicit variables to include |
|
|
221
243
|
|
|
222
244
|
<a name="GetDotenvPreHookCallback"></a>
|
|
223
245
|
|
|
224
|
-
## GetDotenvPreHookCallback ⇒
|
|
246
|
+
## GetDotenvPreHookCallback ⇒ <code>GetDotenvCliOptions</code>
|
|
225
247
|
GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.
|
|
226
248
|
|
|
227
249
|
**Kind**: global typedef
|
|
228
|
-
**Returns**:
|
|
250
|
+
**Returns**: <code>GetDotenvCliOptions</code> - transformed GetDotenv CLI Options object (undefined return value is ignored)
|
|
229
251
|
|
|
230
252
|
| Param | Type | Description |
|
|
231
253
|
| --- | --- | --- |
|
|
232
|
-
| options |
|
|
254
|
+
| options | <code>GetDotenvCliOptions</code> | inbound GetDotenv CLI Options object |
|
|
233
255
|
|
|
234
256
|
<a name="GetDotenvPostHookCallback"></a>
|
|
235
257
|
|
|
@@ -240,50 +262,8 @@ GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv co
|
|
|
240
262
|
|
|
241
263
|
| Param | Type | Description |
|
|
242
264
|
| --- | --- | --- |
|
|
243
|
-
| options | [<code>GetDotenvCliOptions</code>](#GetDotenvCliOptions) | GetDotenv CLI Options object |
|
|
244
265
|
| dotenv | <code>object</code> | dotenv object |
|
|
245
266
|
|
|
246
|
-
<a name="GetDotenvCliConfig"></a>
|
|
247
|
-
|
|
248
|
-
## GetDotenvCliConfig : <code>Object</code>
|
|
249
|
-
GetDotenv CLI Config type
|
|
250
|
-
|
|
251
|
-
**Kind**: global typedef
|
|
252
|
-
**Properties**
|
|
253
|
-
|
|
254
|
-
| Name | Type | Description |
|
|
255
|
-
| --- | --- | --- |
|
|
256
|
-
| [config] | <code>object</code> | config options |
|
|
257
|
-
| [config.defaultOptions] | [<code>GetDotenvCliOptions</code>](#GetDotenvCliOptions) | default options |
|
|
258
|
-
| [config.preHook] | [<code>GetDotenvPreHookCallback</code>](#GetDotenvPreHookCallback) | transforms inbound options & executes side effects |
|
|
259
|
-
| [config.postHook] | [<code>GetDotenvPostHookCallback</code>](#GetDotenvPostHookCallback) | executes side effects within getdotenv context |
|
|
260
|
-
|
|
261
|
-
<a name="OptionsType"></a>
|
|
262
|
-
|
|
263
|
-
## OptionsType : <code>Object</code>
|
|
264
|
-
get-dotenv options type
|
|
265
|
-
|
|
266
|
-
**Kind**: global typedef
|
|
267
|
-
**Properties**
|
|
268
|
-
|
|
269
|
-
| Name | Type | Description |
|
|
270
|
-
| --- | --- | --- |
|
|
271
|
-
| [dotenvToken] | <code>string</code> | token indicating a dotenv file |
|
|
272
|
-
| [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
|
|
273
|
-
| [env] | <code>string</code> | target environment |
|
|
274
|
-
| [excludeDynamic] | <code>bool</code> | exclude dynamic variables |
|
|
275
|
-
| [excludeEnv] | <code>bool</code> | exclude environment-specific variables |
|
|
276
|
-
| [excludeGlobal] | <code>bool</code> | exclude global & dynamic variables |
|
|
277
|
-
| [excludePrivate] | <code>bool</code> | exclude private variables |
|
|
278
|
-
| [excludePublic] | <code>bool</code> | exclude public variables |
|
|
279
|
-
| [loadProcess] | <code>bool</code> | load dotenv to process.env |
|
|
280
|
-
| [log] | <code>bool</code> | log result to logger |
|
|
281
|
-
| [logger] | <code>function</code> | logger function |
|
|
282
|
-
| [outputPath] | <code>string</code> | if populated, writes consolidated .env file to this path (follows [dotenv-expand rules](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env)) |
|
|
283
|
-
| [paths] | <code>Array.<string></code> | array of input directory paths |
|
|
284
|
-
| [privateToken] | <code>string</code> | token indicating private variables |
|
|
285
|
-
| [vars] | <code>object</code> | explicit variables to include |
|
|
286
|
-
|
|
287
267
|
|
|
288
268
|
---
|
|
289
269
|
|
package/bin/getdotenv/index.js
CHANGED
|
@@ -6,20 +6,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.getDotenvSync = exports.getDotenv = void 0;
|
|
7
7
|
var _dotenvExpand = require("dotenv-expand");
|
|
8
8
|
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
9
|
+
var _lodash = _interopRequireDefault(require("lodash.pickby"));
|
|
9
10
|
var _path = _interopRequireDefault(require("path"));
|
|
10
11
|
var _nanoid = require("nanoid");
|
|
11
|
-
var
|
|
12
|
+
var _options = require("./options.js");
|
|
12
13
|
var _readDotenv = require("./readDotenv.js");
|
|
13
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
15
|
// npm imports
|
|
15
16
|
|
|
16
17
|
// lib imports
|
|
17
18
|
|
|
19
|
+
const pruneVars = (getdotenvDefaultOptions, options) => (0, _lodash.default)({
|
|
20
|
+
...(getdotenvDefaultOptions.vars ?? {}),
|
|
21
|
+
...(options.vars ?? {})
|
|
22
|
+
}, v => v === null || v === void 0 ? void 0 : v.length);
|
|
23
|
+
|
|
18
24
|
/**
|
|
19
25
|
* get-dotenv options type
|
|
20
26
|
*
|
|
21
|
-
* @typedef {Object}
|
|
22
|
-
*
|
|
27
|
+
* @typedef {Object} GetDotenvOptions
|
|
23
28
|
* @property {string} [dotenvToken] - token indicating a dotenv file
|
|
24
29
|
* @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
25
30
|
* @property {string} [env] - target environment
|
|
@@ -43,7 +48,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
43
48
|
* @async
|
|
44
49
|
* @function getDotenv
|
|
45
50
|
*
|
|
46
|
-
* @param {
|
|
51
|
+
* @param {GetDotenvOptions} [options] - options object
|
|
47
52
|
*
|
|
48
53
|
* @returns {Promise<object>} The combined parsed dotenv object.
|
|
49
54
|
*/
|
|
@@ -52,8 +57,8 @@ const getDotenv = async function () {
|
|
|
52
57
|
// Apply defaults.
|
|
53
58
|
let {
|
|
54
59
|
dotenvToken,
|
|
55
|
-
env,
|
|
56
60
|
dynamicPath,
|
|
61
|
+
env,
|
|
57
62
|
excludeDynamic,
|
|
58
63
|
excludeEnv,
|
|
59
64
|
excludeGlobal,
|
|
@@ -61,15 +66,15 @@ const getDotenv = async function () {
|
|
|
61
66
|
excludePublic,
|
|
62
67
|
loadProcess,
|
|
63
68
|
log,
|
|
64
|
-
logger,
|
|
69
|
+
logger = console.log,
|
|
65
70
|
outputPath,
|
|
66
71
|
paths,
|
|
67
|
-
privateToken
|
|
68
|
-
vars = {}
|
|
72
|
+
privateToken
|
|
69
73
|
} = {
|
|
70
|
-
...
|
|
74
|
+
..._options.getdotenvDefaultOptions,
|
|
71
75
|
...options
|
|
72
76
|
};
|
|
77
|
+
const vars = pruneVars(_options.getdotenvDefaultOptions, options);
|
|
73
78
|
|
|
74
79
|
// Read .env files.
|
|
75
80
|
const loaded = await paths.reduce(async (e, p) => {
|
|
@@ -101,7 +106,7 @@ const getDotenv = async function () {
|
|
|
101
106
|
});
|
|
102
107
|
|
|
103
108
|
// Process dynamic variables.
|
|
104
|
-
if (dynamicPath && !excludeDynamic) {
|
|
109
|
+
if (dynamicPath && !excludeDynamic && (await _fsExtra.default.exists(dynamicPath))) {
|
|
105
110
|
const dynamic = new Function(`return ${(await _fsExtra.default.readFile(dynamicPath)).toString()}`)()(dotenv);
|
|
106
111
|
Object.keys(dynamic).forEach(key => {
|
|
107
112
|
Object.assign(dotenv, {
|
|
@@ -129,8 +134,8 @@ const getDotenv = async function () {
|
|
|
129
134
|
if (loadProcess) Object.assign(process.env, dotenv, {
|
|
130
135
|
getdotenvOptions: JSON.stringify({
|
|
131
136
|
dotenvToken,
|
|
132
|
-
env,
|
|
133
137
|
dynamicPath,
|
|
138
|
+
env,
|
|
134
139
|
excludeDynamic,
|
|
135
140
|
excludeEnv,
|
|
136
141
|
excludeGlobal,
|
|
@@ -151,7 +156,7 @@ const getDotenv = async function () {
|
|
|
151
156
|
*
|
|
152
157
|
* @function getDotenvSync
|
|
153
158
|
*
|
|
154
|
-
* @param {
|
|
159
|
+
* @param {GetDotenvOptions} [options] - options object
|
|
155
160
|
*
|
|
156
161
|
* @returns {Object} The combined parsed dotenv object.
|
|
157
162
|
*/
|
|
@@ -161,8 +166,8 @@ const getDotenvSync = function () {
|
|
|
161
166
|
// Apply defaults.
|
|
162
167
|
let {
|
|
163
168
|
dotenvToken,
|
|
164
|
-
env,
|
|
165
169
|
dynamicPath,
|
|
170
|
+
env,
|
|
166
171
|
excludeDynamic,
|
|
167
172
|
excludeEnv,
|
|
168
173
|
excludeGlobal,
|
|
@@ -170,14 +175,15 @@ const getDotenvSync = function () {
|
|
|
170
175
|
excludePublic,
|
|
171
176
|
loadProcess,
|
|
172
177
|
log,
|
|
178
|
+
logger = console.log,
|
|
173
179
|
outputPath,
|
|
174
180
|
paths,
|
|
175
|
-
privateToken
|
|
176
|
-
vars
|
|
181
|
+
privateToken
|
|
177
182
|
} = {
|
|
178
|
-
...
|
|
183
|
+
..._options.getdotenvDefaultOptions,
|
|
179
184
|
...options
|
|
180
185
|
};
|
|
186
|
+
const vars = pruneVars(_options.getdotenvDefaultOptions, options);
|
|
181
187
|
|
|
182
188
|
// Read .env files.
|
|
183
189
|
const loaded = paths.reduce((e, p) => {
|
|
@@ -208,7 +214,7 @@ const getDotenvSync = function () {
|
|
|
208
214
|
});
|
|
209
215
|
|
|
210
216
|
// Process dynamic variables.
|
|
211
|
-
if (dynamicPath && !excludeDynamic) {
|
|
217
|
+
if (dynamicPath && !excludeDynamic && _fsExtra.default.existsSync(dynamicPath)) {
|
|
212
218
|
const dynamic = new Function(`return ${_fsExtra.default.readFileSync(dynamicPath).toString()}`)()(dotenv);
|
|
213
219
|
Object.keys(dynamic).forEach(key => {
|
|
214
220
|
Object.assign(dotenv, {
|
|
@@ -230,7 +236,7 @@ const getDotenvSync = function () {
|
|
|
230
236
|
}
|
|
231
237
|
|
|
232
238
|
// Log result.
|
|
233
|
-
if (log)
|
|
239
|
+
if (log) logger(dotenv);
|
|
234
240
|
|
|
235
241
|
// Load process.env.
|
|
236
242
|
if (loadProcess) Object.assign(process.env, dotenv, {
|