@karmaniverous/get-dotenv 3.1.18 → 4.0.0-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 CHANGED
@@ -2,27 +2,44 @@
2
2
 
3
3
  Load environment variables with a cascade of environment-aware dotenv files. You can:
4
4
 
5
- - Load dotenv files synchronously or asynchronously.
6
- - Load variables for a specific environment or none.
7
- - Define dynamic variables progressively in terms of other variables and other logic.
8
- - Exclude public, private, global, environment-specific, or dynamic variables.
9
- - Specify explicit variables to include.
10
- - Extract the resulting variables to an object, `process.env`, a dotenv file, or a logger object, in any combination.
11
- - Execute a shell command within the resulting environment. You can even nest additional `getdotenv` calls!
12
- - Specify the directories containing your dotenv files.
13
- - Specify the filename token that identifies dotenv files (e.g. '.env').
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.
17
-
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.
19
-
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.
5
+ Asynchronously load environment variables from multiple dotenv files.
6
+
7
+ Segregate variables info distinct files:
8
+
9
+ - Public files (e.g. `.env`, `env.dev`, `env.test`) are synced with your git repository.
10
+ - Private files (e.g. `.env.local`, `env.dev.local`, `env.test.local`) are protected by `.gitignore`.
11
+ - Global files (e.g. `.env`, `env.local`) apply to all environments.
12
+ - Env files (e.g. `.env.dev`, `.env.dev.local`, `.env.test`, `.env.test.local`) apply to a specific environment.
13
+ - Dynamic files (`.env.js`) export logic that dynamically & progressively generates new variables or overrides current ones.
14
+
15
+ Dynamically specify which variables to load by type.
16
+
17
+ ✅ Explicitly add variables to the loaded set.
18
+
19
+ ✅ Extract the resulting variables to an object, `process.env`, a dotenv file, or a logger object, in any combination.
20
+
21
+ ✅ Customize your dotenv file directories & naming patterns.
22
+
23
+ ✅ Perform all of the above either programmatically or from the command line, where you can also execute additional shell commands within the resulting context... including nested `getdotenv` commands that inherit the parent command's settings & context!
24
+
25
+ ✅ Set defaults for all options in a `getdotenv.config.json` file in your project root directory.
26
+
27
+ ✅ Generate an extensible `getdotenv`-based CLI for use in your own projects.
28
+
29
+ `getdotenv` relies on the excellent [`dotenv`](https://www.npmjs.com/package/dotenv) parser and somewhat improves on [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) for recursive variable expansion.
21
30
 
22
31
  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.
23
32
 
24
33
  When you plug your own [`commander`](https://www.npmjs.com/package/commander) CLI commands into the `getdotenv` base, they will execute within all of the environmental context created above!
25
34
 
35
+ ## Breaking Changes
36
+
37
+ In version 4.0.0, in addition to a full TypeScript refactor, I replaced the use of the unsafe `Function` constructor for dynamic variable processing with a MUCH safer dynamic module import.
38
+
39
+ Dynamic importing is intrinsically asynchronous, and so far I haven't been able to figure out how to cram that into the synchronous `getDotenvSync` function. There really aren't THAT many users of this library, so rather than have async & sync versions that do different things, I just eliminated the sync version entirely.
40
+
41
+ If you have a use case for sync dotenv processing and DON'T need dynamic variables, let me know and I'll put the restricted version back in. If you have an idea of how to make dynamic imports synchronous, I'm all ears!
42
+
26
43
  ## Installation
27
44
 
28
45
  ```bash
@@ -32,34 +49,30 @@ npm install @karmaniverous/get-dotenv
32
49
  ## Usage
33
50
 
34
51
  ```js
35
- import { getDotenv, getDotenvSync } from '@karmaniverous/get-dotenv';
52
+ import { getDotenv } from '@karmaniverous/get-dotenv';
36
53
 
37
- // asynchronous
38
54
  const dotenv = await getDotenv(options);
39
-
40
- // synchronous
41
- const dotenvSync = await getDotenvSync(options);
42
55
  ```
43
56
 
44
- See [OptionsType](#optionstype--object) below for configuration options.
57
+ See the [GetDotenvOptions](./src/GetDotenvOptions.ts) type for descriptions of all the configuration options.
45
58
 
46
59
  ## Dynamic Processing
47
60
 
48
- This package supports the full [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) syntax.
61
+ This package supports the full [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) syntax, with some internal performance improvements.
49
62
 
50
- Yse the `dynamicPath` option to add a relative path to a Javascript file with a function expression like this:
63
+ Use the `dynamicPath` option to add a relative path to a Javascript file with a default export like this:
51
64
 
52
65
  ```js
53
- (dotenv) => ({
66
+ export default {
54
67
  SOME_DYNAMIC_VARIABLE: (dotenv) => someLogic(dotenv),
55
68
  ANOTHER_DYNAMIC_VARIABLE: (dotenv) =>
56
69
  someOtherLogic(dotenv.SOME_DYNAMIC_VARIABLE),
57
- });
70
+ ONE_MORE_TIME: ({ DESTRUCTRED_VARIABLE, ANOTHER_DYNAMIC_VARIABLE }) =>
71
+ DESTRUCTRED_VARIABLE + ANOTHER_DYNAMIC_VARIABLE,
72
+ };
58
73
  ```
59
74
 
60
- This function should take the expanded `dotenv` object as an argument and return an object. Each object key will be evaluated progressively.
61
-
62
- If the corresponding value is a function, it will be executed with the current state of `dotenv` as its single argument and the result applied back to the `dotenv` object. Otherwise, the value will just be applied back to `dotenv`.
75
+ If the value corresponding to a key is a function, it will be executed with the current state of `dotenv` as its single argument and the result applied back to the `dotenv` object. Otherwise, the value will just be applied back to `dotenv`.
63
76
 
64
77
  Since keys will be evaluated progressively, each successive key function will have access to any previous ones. These keys can also override existing variables.
65
78
 
@@ -71,10 +84,10 @@ Implementation always runs a little behind documentation. These topics & improve
71
84
  - Integrating `getdotenv` into your npm scripts.
72
85
  - Creating a `getdotenv`-based CLI.
73
86
  - Some gotchas & tips around managing your shell execution context.
74
-
87
+
75
88
  # Command Line Interface
76
89
 
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.
90
+ Note that the defaults below can be changed in your own environment by deriving your base CLI using the [`generateDotenvCli`](./src/generateGetDotenvCli.ts) function or placing a `getdotenv.options.json` file in your project root directory.
78
91
 
79
92
  ```text
80
93
  Usage: getdotenv [options] [command]
@@ -82,10 +95,10 @@ Usage: getdotenv [options] [command]
82
95
  Base CLI.
83
96
 
84
97
  Options:
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
98
+ -e, --env <string> target environment (dotenv-expanded)
99
+ -v, --vars <string> extra variables expressed as delimited key-value pairs (dotenv-expanded): KEY1=VAL1 KEY2=VAL2
100
+ -c, --command <string> shell command string (dotenv-expanded)
101
+ -o, --output-path <string> consolidated output file (dotenv-expanded)
89
102
  -p, --load-process load variables to process.env ON (default)
90
103
  -P, --load-process-off load variables to process.env OFF
91
104
  -a, --exclude-all exclude all dotenv variables from loading ON
@@ -104,169 +117,21 @@ Options:
104
117
  -L, --log-off console log loaded variables OFF (default)
105
118
  -d, --debug debug mode ON
106
119
  -D, --debug-off debug mode OFF (default)
107
- --default-env <string> default target environment (default: "dev")
120
+ --default-env <string> default target environment
108
121
  --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")
122
+ --dynamic-path <string> dynamic variables path
123
+ --paths <string> dotenv-expanded delimited list of paths to dotenv directory (default: "./")
111
124
  --paths-delimiter <string> paths delimiter string (default: " ")
112
- --paths-delimiter-pattern <string> paths delimiter regex pattern (default: "\\s+")
125
+ --paths-delimiter-pattern <string> paths delimiter regex pattern
113
126
  --private-token <string> dotenv-expanded token indicating private variables (default: "local")
114
127
  --vars-delimiter <string> vars delimiter string (default: " ")
115
- --vars-delimiter-pattern <string> vars delimiter regex pattern (default: "\\s+")
128
+ --vars-delimiter-pattern <string> vars delimiter regex pattern
116
129
  --vars-assignor <string> vars assignment operator string (default: "=")
117
- --vars-assignor-pattern <string> vars assignment operator regex pattern (default: "=")
130
+ --vars-assignor-pattern <string> vars assignment operator regex pattern
118
131
  -h, --help display help for command
119
132
 
120
133
  Commands:
121
134
  cmd execute shell command string (default command)
122
135
  help [command] display help for command
123
136
  ```
124
-
125
- # API Documentation
126
-
127
- ## Functions
128
-
129
- <dl>
130
- <dt><a href="#getDotenv">getDotenv([options])</a> ⇒ <code>Promise.&lt;object&gt;</code></dt>
131
- <dd><p>Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]</p>
132
- </dd>
133
- <dt><a href="#getDotenvSync">getDotenvSync([options])</a> ⇒ <code>Object</code></dt>
134
- <dd><p>Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]</p>
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>
139
- </dl>
140
-
141
- ## Typedefs
142
-
143
- <dl>
144
- <dt><a href="#GetDotenvOptions">GetDotenvOptions</a> : <code>Object</code></dt>
145
- <dd><p>get-dotenv options type</p>
146
- </dd>
147
- <dt><a href="#GetDotenvPreHookCallback">GetDotenvPreHookCallback</a> ⇒ <code>GetDotenvCliOptions</code></dt>
148
- <dd><p>GetDotenv CLI Pre-hook Callback type. Transforms inbound options &amp; executes side effects.</p>
149
- </dd>
150
- <dt><a href="#GetDotenvPostHookCallback">GetDotenvPostHookCallback</a> : <code>function</code></dt>
151
- <dd><p>GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.</p>
152
- </dd>
153
- </dl>
154
-
155
- <a name="getDotenv"></a>
156
-
157
- ## getDotenv([options]) ⇒ <code>Promise.&lt;object&gt;</code>
158
- Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
159
-
160
- **Kind**: global function
161
- **Returns**: <code>Promise.&lt;object&gt;</code> - The combined parsed dotenv object.
162
-
163
- | Param | Type | Description |
164
- | --- | --- | --- |
165
- | [options] | [<code>GetDotenvOptions</code>](#GetDotenvOptions) | options object |
166
-
167
- <a name="getDotenvSync"></a>
168
-
169
- ## getDotenvSync([options]) ⇒ <code>Object</code>
170
- Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
171
-
172
- **Kind**: global function
173
- **Returns**: <code>Object</code> - The combined parsed dotenv object.
174
-
175
- | Param | Type | Description |
176
- | --- | --- | --- |
177
- | [options] | [<code>GetDotenvOptions</code>](#GetDotenvOptions) | options object |
178
-
179
- <a name="getDotenvCli"></a>
180
-
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
222
-
223
- **Kind**: global typedef
224
- **Properties**
225
-
226
- | Name | Type | Description |
227
- | --- | --- | --- |
228
- | [defaultEnv] | <code>string</code> | default target environment |
229
- | [dotenvToken] | <code>string</code> | token indicating a dotenv file |
230
- | [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
231
- | [env] | <code>string</code> | target environment |
232
- | [excludeDynamic] | <code>bool</code> | exclude dynamic variables |
233
- | [excludeEnv] | <code>bool</code> | exclude environment-specific variables |
234
- | [excludeGlobal] | <code>bool</code> | exclude global & dynamic variables |
235
- | [excludePrivate] | <code>bool</code> | exclude private variables |
236
- | [excludePublic] | <code>bool</code> | exclude public variables |
237
- | [loadProcess] | <code>bool</code> | load dotenv to process.env |
238
- | [log] | <code>bool</code> | log result to logger |
239
- | [logger] | <code>function</code> | logger object (defaults to console) |
240
- | [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)) |
241
- | [paths] | <code>Array.&lt;string&gt;</code> | array of input directory paths |
242
- | [privateToken] | <code>string</code> | token indicating private variables |
243
- | [vars] | <code>object</code> | explicit variables to include |
244
-
245
- <a name="GetDotenvPreHookCallback"></a>
246
-
247
- ## GetDotenvPreHookCallback ⇒ <code>GetDotenvCliOptions</code>
248
- GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.
249
-
250
- **Kind**: global typedef
251
- **Returns**: <code>GetDotenvCliOptions</code> - transformed GetDotenv CLI Options object (undefined return value is ignored)
252
-
253
- | Param | Type | Description |
254
- | --- | --- | --- |
255
- | options | <code>GetDotenvCliOptions</code> | inbound GetDotenv CLI Options object |
256
-
257
- <a name="GetDotenvPostHookCallback"></a>
258
-
259
- ## GetDotenvPostHookCallback : <code>function</code>
260
- GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.
261
-
262
- **Kind**: global typedef
263
-
264
- | Param | Type | Description |
265
- | --- | --- | --- |
266
- | dotenv | <code>object</code> | dotenv object |
267
-
268
-
269
- ---
270
-
271
- See more great templates and other tools on
272
- [my GitHub Profile](https://github.com/karmaniverous)!
137
+