@karmaniverous/get-dotenv 3.1.19 → 4.0.0-1

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/lib/getDotenv.js DELETED
@@ -1,280 +0,0 @@
1
- // npm imports
2
- import fs from 'fs-extra';
3
- import pickBy from 'lodash.pickby';
4
- import path from 'path';
5
- import { nanoid } from 'nanoid';
6
-
7
- // lib imports
8
- import { dotenvExpandAll } from './dotenvExpand.js';
9
- import { getdotenvDefaultOptions } from './options.js';
10
- import { readDotenv, readDotenvSync } from './readDotenv.js';
11
-
12
- const pruneVars = (getdotenvDefaultOptions, options) =>
13
- pickBy(
14
- {
15
- ...(getdotenvDefaultOptions.vars ?? {}),
16
- ...(options.vars ?? {}),
17
- },
18
- (v) => v?.length
19
- );
20
-
21
- /**
22
- * get-dotenv options type
23
- *
24
- * @typedef {Object} GetDotenvOptions
25
- * @property {string} [defaultEnv] - default target environment
26
- * @property {string} [dotenvToken] - token indicating a dotenv file
27
- * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
28
- * @property {string} [env] - target environment
29
- * @property {bool} [excludeDynamic] - exclude dynamic variables
30
- * @property {bool} [excludeEnv] - exclude environment-specific variables
31
- * @property {bool} [excludeGlobal] - exclude global & dynamic variables
32
- * @property {bool} [excludePrivate] - exclude private variables
33
- * @property {bool} [excludePublic] - exclude public variables
34
- * @property {bool} [loadProcess] - load dotenv to process.env
35
- * @property {bool} [log] - log result to logger
36
- * @property {function} [logger] - logger object (defaults to console)
37
- * @property {string} [outputPath] - if populated, writes consolidated .env file to this path (follows {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expand rules})
38
- * @property {string[]} [paths] - array of input directory paths
39
- * @property {string} [privateToken] - token indicating private variables
40
- * @property {object} [vars] - explicit variables to include
41
- */
42
-
43
- /**
44
- * Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
45
- *
46
- * @async
47
- * @function getDotenv
48
- *
49
- * @param {GetDotenvOptions} [options] - options object
50
- *
51
- * @returns {Promise<object>} The combined parsed dotenv object.
52
- */
53
- export const getDotenv = async (options = {}) => {
54
- // Apply defaults.
55
- let {
56
- defaultEnv,
57
- dotenvToken,
58
- dynamicPath,
59
- env,
60
- excludeDynamic,
61
- excludeEnv,
62
- excludeGlobal,
63
- excludePrivate,
64
- excludePublic,
65
- loadProcess,
66
- log,
67
- logger = console,
68
- outputPath,
69
- paths,
70
- privateToken,
71
- } = { ...getdotenvDefaultOptions, ...options };
72
-
73
- const vars = pruneVars(getdotenvDefaultOptions, options);
74
-
75
- // Read .env files.
76
- const loaded = await paths.reduce(async (e, p) => {
77
- let publicGlobal =
78
- excludePublic || excludeGlobal
79
- ? {}
80
- : readDotenv(path.resolve(p, dotenvToken));
81
- let publicEnv =
82
- excludePublic || excludeEnv
83
- ? {}
84
- : readDotenv(path.resolve(p, `${dotenvToken}.${env ?? defaultEnv}`));
85
- let privateGlobal =
86
- excludePrivate || excludeGlobal
87
- ? {}
88
- : readDotenv(path.resolve(p, `${dotenvToken}.${privateToken}`));
89
- let privateEnv =
90
- excludePrivate || excludeEnv
91
- ? {}
92
- : readDotenv(
93
- path.resolve(
94
- p,
95
- `${dotenvToken}.${env ?? defaultEnv}.${privateToken}`
96
- )
97
- );
98
-
99
- [e, publicGlobal, publicEnv, privateGlobal, privateEnv] = await Promise.all(
100
- [e, publicGlobal, publicEnv, privateGlobal, privateEnv]
101
- );
102
-
103
- return {
104
- ...e,
105
- ...publicGlobal,
106
- ...publicEnv,
107
- ...privateGlobal,
108
- ...privateEnv,
109
- };
110
- }, {});
111
-
112
- const outputKey = nanoid();
113
-
114
- const dotenv = dotenvExpandAll({
115
- vars: {
116
- ...loaded,
117
- ...vars,
118
- ...(outputPath ? { [outputKey]: outputPath } : {}),
119
- },
120
- progressive: true,
121
- });
122
-
123
- // Process dynamic variables.
124
- if (dynamicPath && !excludeDynamic && (await fs.exists(dynamicPath))) {
125
- const dynamic = new Function(
126
- `return ${(await fs.readFile(dynamicPath)).toString()}`
127
- )()(dotenv);
128
- Object.keys(dynamic).forEach((key) => {
129
- Object.assign(dotenv, {
130
- [key]:
131
- typeof dynamic[key] === 'function'
132
- ? dynamic[key](dotenv)
133
- : dynamic[key],
134
- });
135
- });
136
- }
137
-
138
- // Write output file.
139
- if (outputPath) {
140
- outputPath = dotenv[outputKey];
141
- delete dotenv[outputKey];
142
-
143
- await fs.writeFile(
144
- outputPath,
145
- Object.keys(dotenv).reduce((contents, key) => {
146
- const value = dotenv[key] ?? '';
147
- return `${contents}${key}=${
148
- value.includes('\n') ? `"${value}"` : value
149
- }\n`;
150
- }, ''),
151
- { encoding: 'utf-8' }
152
- );
153
- }
154
-
155
- // Log result.
156
- if (log) logger.log(dotenv);
157
-
158
- // Load process.env.
159
- if (loadProcess) Object.assign(process.env, dotenv);
160
-
161
- return dotenv;
162
- };
163
-
164
- /**
165
- * Synchronously process dotenv files of the form .env[.<ENV>][.<PRIVATETOKEN>]
166
- *
167
- * @function getDotenvSync
168
- *
169
- * @param {GetDotenvOptions} [options] - options object
170
- *
171
- * @returns {Object} The combined parsed dotenv object.
172
- */
173
- export const getDotenvSync = (options = {}) => {
174
- // Apply defaults.
175
- let {
176
- defaultEnv,
177
- dotenvToken,
178
- dynamicPath,
179
- env,
180
- excludeDynamic,
181
- excludeEnv,
182
- excludeGlobal,
183
- excludePrivate,
184
- excludePublic,
185
- loadProcess,
186
- log,
187
- logger = console,
188
- outputPath,
189
- paths,
190
- privateToken,
191
- } = { ...getdotenvDefaultOptions, ...options };
192
-
193
- const vars = pruneVars(getdotenvDefaultOptions, options);
194
-
195
- // Read .env files.
196
- const loaded = paths.reduce((e, p) => {
197
- let publicGlobal =
198
- excludePublic || excludeGlobal
199
- ? {}
200
- : readDotenvSync(path.resolve(p, dotenvToken));
201
- let publicEnv =
202
- excludePublic || excludeEnv
203
- ? {}
204
- : readDotenvSync(
205
- path.resolve(p, `${dotenvToken}.${env ?? defaultEnv}`)
206
- );
207
- let privateGlobal =
208
- excludePrivate || excludeGlobal
209
- ? {}
210
- : readDotenvSync(path.resolve(p, `${dotenvToken}.${privateToken}`));
211
- let privateEnv =
212
- excludePrivate || excludeEnv
213
- ? {}
214
- : readDotenvSync(
215
- path.resolve(
216
- p,
217
- `${dotenvToken}.${env ?? defaultEnv}.${privateToken}`
218
- )
219
- );
220
-
221
- return {
222
- ...e,
223
- ...publicGlobal,
224
- ...publicEnv,
225
- ...privateGlobal,
226
- ...privateEnv,
227
- };
228
- }, {});
229
-
230
- const outputKey = nanoid();
231
-
232
- const dotenv = dotenvExpandAll({
233
- vars: {
234
- ...loaded,
235
- ...vars,
236
- ...(outputPath ? { [outputKey]: outputPath } : {}),
237
- },
238
- progressive: true,
239
- });
240
-
241
- // Process dynamic variables.
242
- if (dynamicPath && !excludeDynamic && fs.existsSync(dynamicPath)) {
243
- const dynamic = new Function(
244
- `return ${fs.readFileSync(dynamicPath).toString()}`
245
- )()(dotenv);
246
- Object.keys(dynamic).forEach((key) => {
247
- Object.assign(dotenv, {
248
- [key]:
249
- typeof dynamic[key] === 'function'
250
- ? dynamic[key](dotenv)
251
- : dynamic[key],
252
- });
253
- });
254
- }
255
-
256
- // Write output file.
257
- if (outputPath) {
258
- outputPath = dotenv[outputKey];
259
- delete dotenv[outputKey];
260
-
261
- fs.writeFileSync(
262
- outputPath,
263
- Object.keys(dotenv).reduce((contents, key) => {
264
- const value = dotenv[key] ?? '';
265
- return `${contents}${key}=${
266
- value.includes('\n') ? `"${value}"` : value
267
- }\n`;
268
- }, ''),
269
- { encoding: 'utf-8' }
270
- );
271
- }
272
-
273
- // Log result.
274
- if (log) logger.log(dotenv);
275
-
276
- // Load process.env.
277
- if (loadProcess) Object.assign(process.env, dotenv);
278
-
279
- return dotenv;
280
- };