@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.
@@ -1,475 +0,0 @@
1
- // npm imports
2
- import { Command, Option } from 'commander';
3
- import { execaCommand } from 'execa';
4
- import Os from 'os';
5
-
6
- // lib imports
7
- import { dotenvExpand } from './dotenvExpand.js';
8
- import { getDotenv } from './getDotenv.js';
9
- import {
10
- cli2getdotenvOptions,
11
- cliDefaultOptionsGlobal,
12
- cliDefaultOptionsLocal,
13
- } from './options.js';
14
-
15
- // Discover platform.
16
- const platform = Os.platform();
17
-
18
- /**
19
- * GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.
20
- *
21
- * @async
22
- * @callback GetDotenvPreHookCallback
23
- * @param {GetDotenvCliOptions} options - inbound GetDotenv CLI Options object
24
- * @returns {GetDotenvCliOptions} transformed GetDotenv CLI Options object (undefined return value is ignored)
25
- */
26
-
27
- /**
28
- * GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.
29
- *
30
- * @async
31
- * @callback GetDotenvPostHookCallback
32
- * @param {object} dotenv - dotenv object
33
- */
34
-
35
- /**
36
- * Generate a CLI for get-dotenv.
37
- *
38
- * @function getDotenvCli
39
- * @param {object} [options] - options object
40
- * @param {string} [options.alias] - cli alias (used for cli help)
41
- * @param {string} [options.command] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} shell command string
42
- * @param {bool} [options.debug] - debug mode
43
- * @param {string} [options.defaultEnv] - default target environment
44
- * @param {string} [options.description] - cli description (used for cli help)
45
- * @param {string} [options.dotenvToken] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} token indicating a dotenv file
46
- * @param {string} [options.dynamicPath] - path to file exporting an object keyed to dynamic variable functions
47
- * @param {bool} [options.excludeDynamic] - exclude dynamic dotenv variables
48
- * @param {bool} [options.excludeEnv] - exclude environment-specific dotenv variables
49
- * @param {bool} [options.excludeGlobal] - exclude global dotenv variables
50
- * @param {bool} [options.excludePrivate] - exclude private dotenv variables
51
- * @param {bool} [options.excludePublic] - exclude public dotenv variables
52
- * @param {bool} [options.loadProcess] - load variables to process.env
53
- * @param {bool} [options.log] - log result to console
54
- * @param {function} [options.logger] - logger function
55
- * @param {string} [options.outputPath] - consolidated output file, {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} using loaded env vars
56
- * @param {string} [options.paths] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} delimited list of input directory paths
57
- * @param {string} [options.pathsDelimiter] - paths delimiter string
58
- * @param {string} [options.pathsDelimiterPattern] - paths delimiter regex pattern
59
- * @param {GetDotenvPreHookCallback} [config.preHook] - transforms cli options & executes side effects
60
- * @param {string} [options.privateToken] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} token indicating private variables
61
- * @param {GetDotenvPostHookCallback} [config.postHook] - executes side effects within getdotenv context
62
- * @param {string} [options.vars] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} delimited list of explicit environment variable key-value pairs
63
- * @param {string} [options.varsAssignor] - variable key-value assignor string
64
- * @param {string} [options.varsAssignorPattern] - variable key-value assignor regex pattern
65
- * @param {string} [options.varsDelimiter] - variable key-value pair delimiter string
66
- * @param {string} [options.varsDelimiterPattern] - variable key-value pair delimiter regex pattern
67
- * @returns {object} The CLI command.
68
- */
69
- export const getDotenvCli = ({
70
- logger = console,
71
- preHook,
72
- postHook,
73
- ...cliOptionsCustom
74
- } = {}) => {
75
- const {
76
- alias,
77
- command,
78
- debug,
79
- defaultEnv,
80
- description,
81
- dotenvToken,
82
- dynamicPath,
83
- env,
84
- excludeDynamic,
85
- excludeEnv,
86
- excludeGlobal,
87
- excludePrivate,
88
- excludePublic,
89
- loadProcess,
90
- log,
91
- outputPath,
92
- paths,
93
- pathsDelimiter,
94
- pathsDelimiterPattern,
95
- privateToken,
96
- varsAssignor,
97
- varsAssignorPattern,
98
- varsDelimiter,
99
- varsDelimiterPattern,
100
- } = {
101
- ...cliDefaultOptionsGlobal,
102
- ...cliOptionsCustom,
103
- ...cliDefaultOptionsLocal,
104
- };
105
-
106
- const excludeAll =
107
- excludeDynamic &&
108
- ((excludeEnv && excludeGlobal) || (excludePrivate && excludePublic));
109
-
110
- return new Command()
111
- .name(alias)
112
- .description(description)
113
- .enablePositionalOptions()
114
- .passThroughOptions()
115
- .option('-e, --env <string>', 'target environment', dotenvExpand, env)
116
- .option(
117
- '-v, --vars <string>',
118
- `dotenv-expanded delimited key-value pairs: ${[
119
- ['KEY1', 'VAL1'],
120
- ['KEY2', 'VAL2'],
121
- ]
122
- .map((v) => v.join(varsAssignor))
123
- .join(varsDelimiter)}`,
124
- dotenvExpand
125
- )
126
- .option(
127
- '-c, --command <string>',
128
- 'dotenv-expanded shell command string',
129
- dotenvExpand,
130
- command
131
- )
132
- .option(
133
- '-o, --output-path <string>',
134
- 'consolidated output file, follows dotenv-expand rules using loaded env vars',
135
- dotenvExpand,
136
- outputPath
137
- )
138
- .addOption(
139
- new Option(
140
- '-p, --load-process',
141
- `load variables to process.env ON${loadProcess ? ' (default)' : ''}`
142
- ).conflicts('loadProcessOff')
143
- )
144
- .addOption(
145
- new Option(
146
- '-P, --load-process-off',
147
- `load variables to process.env OFF${!loadProcess ? ' (default)' : ''}`
148
- ).conflicts('loadProcess')
149
- )
150
- .addOption(
151
- new Option(
152
- '-a, --exclude-all',
153
- `exclude all dotenv variables from loading ON${
154
- excludeAll ? ' (default)' : ''
155
- }`
156
- ).conflicts('excludeAllOff')
157
- )
158
- .addOption(
159
- new Option(
160
- '-A, --exclude-all-off',
161
- `exclude all dotenv variables from loading OFF${
162
- !excludeAll ? ' (default)' : ''
163
- }`
164
- ).conflicts('excludeAll')
165
- )
166
- .addOption(
167
- new Option(
168
- '-z, --exclude-dynamic',
169
- `exclude dynamic dotenv variables from loading ON${
170
- excludeDynamic ? ' (default)' : ''
171
- }`
172
- ).conflicts('excludeDynamicOff')
173
- )
174
- .addOption(
175
- new Option(
176
- '-Z, --exclude-dynamic-off',
177
- `exclude dynamic dotenv variables from loading OFF${
178
- !excludeDynamic ? ' (default)' : ''
179
- }`
180
- ).conflicts('excludeDynamic')
181
- )
182
- .addOption(
183
- new Option(
184
- '-n, --exclude-env',
185
- `exclude environment-specific dotenv variables from loading${
186
- excludeEnv ? ' (default)' : ''
187
- }`
188
- ).conflicts('excludeEnvOff')
189
- )
190
- .addOption(
191
- new Option(
192
- '-N, --exclude-env-off',
193
- `exclude environment-specific dotenv variables from loading OFF${
194
- !excludeEnv ? ' (default)' : ''
195
- }`
196
- ).conflicts('excludeEnv')
197
- )
198
- .addOption(
199
- new Option(
200
- '-g, --exclude-global',
201
- `exclude global dotenv variables from loading ON${
202
- excludeGlobal ? ' (default)' : ''
203
- }`
204
- ).conflicts('excludeGlobalOff')
205
- )
206
- .addOption(
207
- new Option(
208
- '-G, --exclude-global-off',
209
- `exclude global dotenv variables from loading OFF${
210
- !excludeGlobal ? ' (default)' : ''
211
- }`
212
- ).conflicts('excludeGlobal')
213
- )
214
- .addOption(
215
- new Option(
216
- '-r, --exclude-private',
217
- `exclude private dotenv variables from loading ON${
218
- excludePrivate ? ' (default)' : ''
219
- }`
220
- ).conflicts('excludePrivateOff')
221
- )
222
- .addOption(
223
- new Option(
224
- '-R, --exclude-private-off',
225
- `exclude private dotenv variables from loading OFF${
226
- !excludePrivate ? ' (default)' : ''
227
- }`
228
- ).conflicts('excludePrivate')
229
- )
230
- .addOption(
231
- new Option(
232
- '-u, --exclude-public',
233
- `exclude public dotenv variables from loading ON${
234
- excludePublic ? ' (default)' : ''
235
- }`
236
- ).conflicts('excludePublicOff')
237
- )
238
- .addOption(
239
- new Option(
240
- '-U, --exclude-public-off',
241
- `exclude public dotenv variables from loading OFF${
242
- !excludePublic ? ' (default)' : ''
243
- }`
244
- ).conflicts('excludePublic')
245
- )
246
- .addOption(
247
- new Option(
248
- '-l, --log',
249
- `console log loaded variables ON${log ? ' (default)' : ''}`
250
- ).conflicts('logOff')
251
- )
252
- .addOption(
253
- new Option(
254
- '-L, --log-off',
255
- `console log loaded variables OFF${!log ? ' (default)' : ''}`
256
- ).conflicts('log')
257
- )
258
- .addOption(
259
- new Option(
260
- '-d, --debug',
261
- `debug mode ON${debug ? ' (default)' : ''}`
262
- ).conflicts('debugOff')
263
- )
264
- .addOption(
265
- new Option(
266
- '-D, --debug-off',
267
- `debug mode OFF${!debug ? ' (default)' : ''}`
268
- ).conflicts('debug')
269
- )
270
- .option(
271
- '--default-env <string>',
272
- 'default target environment',
273
- dotenvExpand,
274
- defaultEnv
275
- )
276
- .option(
277
- '--dotenv-token <string>',
278
- 'dotenv-expanded token indicating a dotenv file',
279
- dotenvExpand,
280
- dotenvToken
281
- )
282
- .option(
283
- '--dynamic-path <string>',
284
- 'dynamic variables path',
285
- dotenvExpand,
286
- dynamicPath
287
- )
288
- .option(
289
- '--paths <string>',
290
- 'dotenv-expanded delimited list of paths to dotenv directory',
291
- dotenvExpand,
292
- paths
293
- )
294
- .option(
295
- '--paths-delimiter <string>',
296
- 'paths delimiter string',
297
- pathsDelimiter
298
- )
299
- .option(
300
- '--paths-delimiter-pattern <string>',
301
- 'paths delimiter regex pattern',
302
- pathsDelimiterPattern
303
- )
304
- .option(
305
- '--private-token <string>',
306
- 'dotenv-expanded token indicating private variables',
307
- dotenvExpand,
308
- privateToken
309
- )
310
- .option('--vars-delimiter <string>', 'vars delimiter string', varsDelimiter)
311
- .option(
312
- '--vars-delimiter-pattern <string>',
313
- 'vars delimiter regex pattern',
314
- varsDelimiterPattern
315
- )
316
- .option(
317
- '--vars-assignor <string>',
318
- 'vars assignment operator string',
319
- varsAssignor
320
- )
321
- .option(
322
- '--vars-assignor-pattern <string>',
323
- 'vars assignment operator regex pattern',
324
- varsAssignorPattern
325
- )
326
- .addCommand(
327
- new Command()
328
- .name('cmd')
329
- .description('execute shell command string (default command)')
330
- .configureHelp({ showGlobalOptions: true })
331
- .enablePositionalOptions()
332
- .passThroughOptions()
333
- .action(async (options, command) => {
334
- const { args } = command;
335
- if (args.length)
336
- await execaCommand(args.join(platform === 'win32' ? '\\ ' : ' '), {
337
- stdio: 'inherit',
338
- shell: true,
339
- });
340
- }),
341
- { isDefault: true }
342
- )
343
- .hook('preSubcommand', async (thisCommand) => {
344
- const rawOptions = thisCommand.opts();
345
-
346
- if (rawOptions.debug)
347
- logger.log('\n*** raw cli options ***\n', { rawOptions });
348
-
349
- // Load options.
350
- let {
351
- command,
352
- debugOff,
353
- excludeAll,
354
- excludeAllOff,
355
- excludeDynamicOff,
356
- excludeEnvOff,
357
- excludeGlobalOff,
358
- excludePrivateOff,
359
- excludePublicOff,
360
- loadProcessOff,
361
- logOff,
362
- ...cliOptions
363
- } = rawOptions;
364
-
365
- // Resolve flags.
366
- const resolveExclusion = (exclude, excludeOff, defaultValue) =>
367
- exclude ? true : excludeOff ? false : defaultValue;
368
-
369
- const resolveExclusionAll = (exclude, excludeOff, defaultValue) =>
370
- excludeAll && !excludeOff
371
- ? true
372
- : excludeAllOff && !exclude
373
- ? false
374
- : defaultValue;
375
-
376
- cliOptions.debug = resolveExclusion(cliOptions.debug, debugOff, debug);
377
-
378
- cliOptions.excludeDynamic = resolveExclusionAll(
379
- cliOptions.excludeDynamic,
380
- excludeDynamicOff,
381
- excludeDynamic
382
- );
383
-
384
- cliOptions.excludeEnv = resolveExclusionAll(
385
- cliOptions.excludeEnv,
386
- excludeEnvOff,
387
- excludeEnv
388
- );
389
-
390
- cliOptions.excludeGlobal = resolveExclusionAll(
391
- cliOptions.excludeGlobal,
392
- excludeGlobalOff,
393
- excludeGlobal
394
- );
395
-
396
- cliOptions.excludePrivate = resolveExclusionAll(
397
- cliOptions.excludePrivate,
398
- excludePrivateOff,
399
- excludePrivate
400
- );
401
-
402
- cliOptions.excludePublic = resolveExclusionAll(
403
- cliOptions.excludePublic,
404
- excludePublicOff,
405
- excludePublic
406
- );
407
-
408
- cliOptions.log = resolveExclusion(cliOptions.log, logOff, log);
409
-
410
- cliOptions.loadProcess = resolveExclusion(
411
- cliOptions.loadProcess,
412
- loadProcessOff,
413
- loadProcess
414
- );
415
-
416
- if (cliOptions.debug)
417
- logger.log('\n*** cli options after default resolution ***\n', {
418
- cliOptions,
419
- });
420
-
421
- // Execute pre-hook.
422
- if (preHook) {
423
- cliOptions = (await preHook(cliOptions)) ?? cliOptions;
424
- if (cliOptions.debug)
425
- logger.log('\n*** cli options after pre-hook ***\n', cliOptions);
426
- }
427
-
428
- // Get getdotenv options from parent command.
429
- const parentGetdotenvOptions = process.env.getdotenvOptions
430
- ? JSON.parse(process.env.getdotenvOptions)
431
- : {};
432
-
433
- const cliGetdotenvOptions = cli2getdotenvOptions(cliOptions);
434
-
435
- const getdotenvOptions = {
436
- ...parentGetdotenvOptions,
437
- ...cliGetdotenvOptions,
438
- };
439
-
440
- if (cliOptions.debug)
441
- logger.log('\n*** getdotenv option resolution ***\n', {
442
- parentGetdotenvOptions,
443
- cliGetdotenvOptions,
444
- getdotenvOptions,
445
- });
446
-
447
- // Execute getdotenv.
448
- thisCommand.getdotenvOptions = {
449
- ...getdotenvOptions,
450
- logger,
451
- };
452
-
453
- const dotenv = await getDotenv(thisCommand.getdotenvOptions);
454
-
455
- if (cliOptions.debug)
456
- logger.log('\n*** resulting dotenv values ***\n', { dotenv });
457
-
458
- // Execute post-hook.
459
- if (postHook) await postHook(dotenv);
460
-
461
- // Execute shell command.
462
- if (command)
463
- await execaCommand(
464
- platform === 'win32' ? command.replace(/ /g, '\\ ') : command,
465
- {
466
- env: {
467
- ...process.env,
468
- getdotenvOptions: JSON.stringify(getdotenvOptions),
469
- },
470
- shell: true,
471
- stdio: 'inherit',
472
- }
473
- );
474
- });
475
- };
package/lib/index.js DELETED
@@ -1,3 +0,0 @@
1
- export { dotenvExpand, dotenvExpandAll } from './dotenvExpand.js';
2
- export { getDotenv, getDotenvSync } from './getDotenv.js';
3
- export { getDotenvCli } from './getDotenvCli.js';
package/lib/options.js DELETED
@@ -1,80 +0,0 @@
1
- // npm imports
2
- import fs from 'fs-extra';
3
- import fromPairs from 'lodash.frompairs';
4
- import pick from 'lodash.pick';
5
- import { packageDirectory } from 'pkg-dir';
6
- import { dirname, resolve } from 'path';
7
- import { fileURLToPath } from 'url';
8
-
9
- const __dirname = dirname(fileURLToPath(import.meta.url));
10
-
11
- // Load default options.
12
- const cliDefaultOptionsGlobalPath = resolve(
13
- __dirname,
14
- '../getdotenv.config.json'
15
- );
16
-
17
- export const cliDefaultOptionsGlobal = (await fs.exists(
18
- cliDefaultOptionsGlobalPath
19
- ))
20
- ? JSON.parse(await fs.readFile(cliDefaultOptionsGlobalPath))
21
- : {};
22
-
23
- const cliDefaultOptionsLocalPath = resolve(
24
- await packageDirectory(),
25
- 'getdotenv.config.json'
26
- );
27
-
28
- export const cliDefaultOptionsLocal = (await fs.exists(
29
- cliDefaultOptionsLocalPath
30
- ))
31
- ? JSON.parse(await fs.readFile(cliDefaultOptionsLocalPath))
32
- : {};
33
-
34
- export const cli2getdotenvOptions = ({
35
- paths,
36
- pathsDelimiter,
37
- pathsDelimiterPattern,
38
- vars,
39
- varsAssignor,
40
- varsAssignorPattern,
41
- varsDelimiter,
42
- varsDelimiterPattern,
43
- ...rest
44
- } = {}) => ({
45
- ...pick(rest, [
46
- 'defaultEnv',
47
- 'dotenvToken',
48
- 'dynamicPath',
49
- 'env',
50
- 'excludeDynamic',
51
- 'excludeEnv',
52
- 'excludeGlobal',
53
- 'excludePrivate',
54
- 'excludePublic',
55
- 'loadProcess',
56
- 'log',
57
- 'outputPath',
58
- 'privateToken',
59
- ]),
60
- paths:
61
- paths?.split(
62
- pathsDelimiterPattern ? RegExp(pathsDelimiterPattern) : pathsDelimiter
63
- ) ?? [],
64
- vars: fromPairs(
65
- vars
66
- ?.split(
67
- varsDelimiterPattern ? RegExp(varsDelimiterPattern) : varsDelimiter
68
- )
69
- .map((v) =>
70
- v.split(
71
- varsAssignorPattern ? RegExp(varsAssignorPattern) : varsAssignor
72
- )
73
- )
74
- ),
75
- });
76
-
77
- export const getdotenvDefaultOptions = cli2getdotenvOptions({
78
- ...cliDefaultOptionsGlobal,
79
- ...cliDefaultOptionsLocal,
80
- });
package/lib/readDotenv.js DELETED
@@ -1,39 +0,0 @@
1
- import { parse } from 'dotenv';
2
- import fs from 'fs-extra';
3
-
4
- /**
5
- * Asynchronously read a dotenv file & parse it into an object.
6
- *
7
- * @async
8
- * @private
9
- * @function readDotenv
10
- *
11
- * @param {string} path - Any value.
12
- *
13
- * @returns {Object} The parsed dotenv object.
14
- */
15
- export const readDotenv = async (path) => {
16
- try {
17
- return (await fs.exists(path)) ? parse(await fs.readFile(path)) : {};
18
- } catch {
19
- return {};
20
- }
21
- };
22
-
23
- /**
24
- * Synchronously reads a dotenv file & parses it into an object.
25
- *
26
- * @private
27
- * @function readDotenvSync
28
- *
29
- * @param {string} path - Any value.
30
- *
31
- * @returns {Object} The parsed dotenv object.
32
- */
33
- export const readDotenvSync = (path) => {
34
- try {
35
- return fs.existsSync(path) ? parse(fs.readFileSync(path)) : {};
36
- } catch {
37
- return {};
38
- }
39
- };