@karmaniverous/get-dotenv 3.0.6 → 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.
@@ -1,199 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getCli = void 0;
7
- var _boolean = require("boolean");
8
- var _commander = require("commander");
9
- var _execa = require("execa");
10
- var _lodash = _interopRequireDefault(require("lodash.frompairs"));
11
- var _lodash2 = _interopRequireDefault(require("lodash.isstring"));
12
- var _dotenvDefaults = require("./dotenvDefaults.js");
13
- var _dotenvExpand = require("./dotenvExpand.js");
14
- var _getDotenv = require("./getDotenv.js");
15
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
- // npm imports
17
-
18
- // lib imports
19
-
20
- const booleanExpand = value => (0, _boolean.boolean)((0, _dotenvExpand.dotenvExpand)(value));
21
-
22
- /**
23
- * GetDotenv CLI Options type
24
- *
25
- * @typedef {Object} GetDotenvCliOptions
26
- * @property {string} [cliInvocation] - cli invocation string (used for cli help)
27
- * @property {bool} [debug] - debug mode
28
- * @property {string} [defaultEnv] - default target environment
29
- * @property {string} [dotenvToken] - token indicating a dotenv file
30
- * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
31
- * @property {string} [env] - target environment
32
- * @property {bool} [excludeDynamic] - exclude dynamic variables
33
- * @property {bool} [excludeEnv] - exclude environment-specific variables
34
- * @property {bool} [excludeGlobal] - exclude global & dynamic variables
35
- * @property {bool} [excludePrivate] - exclude private variables
36
- * @property {bool} [excludePublic] - exclude public variables
37
- * @property {bool} [log] - log result to console
38
- * @property {function} [logger] - logger function
39
- * @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})
40
- * @property {string} [paths] - space-delimited list of input directory paths
41
- * @property {string} [privateToken] - token indicating private variables.
42
- * @property {bool} [suppressDotenv] - suppress dotenv loading
43
- */
44
-
45
- /**
46
- * GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.
47
- *
48
- * @async
49
- * @callback GetDotenvPreHookCallback
50
- * @param {GetDotenvCliOptions} options - inbound GetDotenv CLI Options object
51
- * @returns {GetDotenvCliOptions} transformed GetDotenv CLI Options object (undefined return value is ignored)
52
- */
53
-
54
- /**
55
- * GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.
56
- *
57
- * @async
58
- * @callback GetDotenvPostHookCallback
59
- * @param {GetDotenvCliOptions} options - GetDotenv CLI Options object
60
- * @param {object} dotenv - dotenv object
61
- */
62
-
63
- /**
64
- * GetDotenv CLI Config type
65
- *
66
- * @typedef {Object} GetDotenvCliConfig
67
- * @property {object} [config] - config options
68
- * @property {GetDotenvCliOptions} [config.defaultOptions] - default options
69
- * @property {GetDotenvPreHookCallback} [config.preHook] - transforms inbound options & executes side effects
70
- * @property {GetDotenvPostHookCallback} [config.postHook] - executes side effects within getdotenv context
71
- */
72
-
73
- /**
74
- * Generate a CLI for get-dotenv.
75
- *
76
- * @param {GetDotenvCliConfig} [config] - config object
77
- * @returns {object} The CLI command.
78
- */
79
- const getCli = function () {
80
- let {
81
- defaultOptions = {},
82
- preHook,
83
- postHook
84
- } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
85
- let {
86
- cliInvocation = 'getdotenv',
87
- command,
88
- defaultEnv = 'dev',
89
- dotenvToken,
90
- dynamicPath,
91
- env,
92
- excludeDynamic,
93
- excludeEnv,
94
- excludeGlobal,
95
- excludePrivate,
96
- excludePublic,
97
- log,
98
- outputPath,
99
- paths,
100
- pathsDelimiter = '\\s+',
101
- privateToken,
102
- suppressDotenv,
103
- vars,
104
- varsAssignor = '=',
105
- varsDelimiter = '\\s+'
106
- } = {
107
- ..._dotenvDefaults.dotenvDefaults,
108
- ...defaultOptions
109
- };
110
- if (Array.isArray(paths)) paths = paths.join(' ');
111
- return new _commander.Command().name(cliInvocation).description('Base CLI. All options except delimiters follow dotenv-expand rules.').enablePositionalOptions().passThroughOptions().option('-e, --env <string>', 'target environment', _dotenvExpand.dotenvExpand, env).option('--default-env <string>', 'default target environment', _dotenvExpand.dotenvExpand, defaultEnv).option('-p, --paths <string>', 'delimited list of paths to dotenv directory', _dotenvExpand.dotenvExpand, paths).option('--paths-delimiter <string>', 'regex paths delimiter', pathsDelimiter).option('-v, --vars <string>', 'delimited list KEY=VALUE pairs', _dotenvExpand.dotenvExpand, vars).option('--vars-delimiter <string>', 'regex vars delimiter', varsDelimiter).option('--vars-assignor <string>', 'regex vars assignment operator', varsAssignor).option('-y, --dynamic-path <string>', 'dynamic variables path', _dotenvExpand.dotenvExpand, dynamicPath).option('-o, --output-path <string>', 'consolidated output file, follows dotenv-expand rules using loaded env vars', _dotenvExpand.dotenvExpand, outputPath).addOption(new _commander.Option('-n, --exclude-env', `exclude environment-specific variables${excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnvOff')).addOption(new _commander.Option('-N, --exclude-env-off', `exclude environment-specific variables OFF${!excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnv')).addOption(new _commander.Option('-g, --exclude-global', `exclude global variables${excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobalOff')).addOption(new _commander.Option('-G, --exclude-global-off', `exclude global variables OFF${!excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobal')).addOption(new _commander.Option('-r, --exclude-private', `exclude private variables${excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivateOff')).addOption(new _commander.Option('-R, --exclude-private-off', `exclude private variables OFF${!excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivate')).addOption(new _commander.Option('-u, --exclude-public', `exclude public variables${excludePublic ? ' (default)' : ''}`).conflicts('excludePublicOff')).addOption(new _commander.Option('-U, --exclude-public-off', `exclude public variables OFF${!excludePublic ? ' (default)' : ''}`).conflicts('excludePublic')).addOption(new _commander.Option('-z, --exclude-dynamic', `exclude dynamic variables${excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamicOff')).addOption(new _commander.Option('-Z, --exclude-dynamic-off', `exclude dynamic variables OFF${!excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamic')).addOption(new _commander.Option('-l, --log', `console log extracted variables${log ? ' (default)' : ''}`).conflicts('logOff')).addOption(new _commander.Option('-L, --log-off', `console log extracted variables OFF${!log ? ' (default)' : ''}`).conflicts('log')).option('-x, --suppress-dotenv', 'suppress dotenv loading', booleanExpand, suppressDotenv ?? false).option('-c, --command <string>', 'shell command string', _dotenvExpand.dotenvExpand, command).option('--dotenv-token <string>', 'token indicating a dotenv file', _dotenvExpand.dotenvExpand, dotenvToken).option('--private-token <string>', 'token indicating private variables', _dotenvExpand.dotenvExpand, privateToken).option('-D, --debug', 'debug mode').addCommand(new _commander.Command().name('cmd').description('execute shell command string (default command)').configureHelp({
112
- showGlobalOptions: true
113
- }).enablePositionalOptions().passThroughOptions().action(async (options, _ref) => {
114
- let {
115
- args
116
- } = _ref;
117
- if (args.length) await (0, _execa.execaCommand)(args.join('\\ '), {
118
- stdio: 'inherit',
119
- shell: true
120
- });
121
- }), {
122
- isDefault: true
123
- }).hook('preSubcommand', async thisCommand => {
124
- var _paths;
125
- // Inherit options from parent command.
126
- const getdotenvOptions = process.env['getdotenvOptions'] ? JSON.parse(process.env['getdotenvOptions']) : {};
127
- const {
128
- excludeDynamicOff,
129
- excludeEnvOff,
130
- excludeGlobalOff,
131
- excludePrivateOff,
132
- excludePublicOff,
133
- logOff,
134
- ...localOptions
135
- } = thisCommand.opts();
136
- if (excludeDynamicOff) localOptions.excludeDynamic = false;
137
- if (excludeEnvOff) localOptions.excludeEnv = false;
138
- if (excludeGlobalOff) localOptions.excludeGlobal = false;
139
- if (excludePrivateOff) localOptions.excludePrivate = false;
140
- if (excludePublicOff) localOptions.excludePublic = false;
141
- if (logOff) localOptions.log = false;
142
- let mergedOptions = {
143
- ...getdotenvOptions,
144
- ...localOptions
145
- };
146
-
147
- // Execute pre-hook.
148
- const options = preHook ? (await preHook(mergedOptions)) ?? mergedOptions : mergedOptions;
149
-
150
- // Get options.
151
- let {
152
- command,
153
- debug,
154
- defaultEnv,
155
- env,
156
- paths,
157
- pathsDelimiter,
158
- suppressDotenv,
159
- vars: varsStr,
160
- varsDelimiter,
161
- varsAssignor,
162
- ...rest
163
- } = options;
164
-
165
- // Parse vars.
166
- const vars = varsStr !== null && varsStr !== void 0 && varsStr.length ? (0, _lodash.default)(varsStr.split(new RegExp(varsDelimiter)).map(v => v.split(new RegExp(varsAssignor)))) : undefined;
167
- if (debug) {
168
- console.log('*** getdotenvOptions ***', getdotenvOptions);
169
- console.log('*** localOptions ***', localOptions);
170
- console.log('*** mergedOptions ***', mergedOptions);
171
- console.log('*** options ***', options);
172
- console.log('*** vars ***', vars);
173
- }
174
-
175
- // Execute getdotenv.
176
- if ((_paths = paths) !== null && _paths !== void 0 && _paths.length && !suppressDotenv) {
177
- var _paths2;
178
- if ((0, _lodash2.default)(paths)) paths = (_paths2 = paths) === null || _paths2 === void 0 ? void 0 : _paths2.split(new RegExp(pathsDelimiter));
179
- var dotenv = await (0, _getDotenv.getDotenv)({
180
- ...rest,
181
- env: env ?? defaultEnv,
182
- loadProcess: true,
183
- paths,
184
- vars
185
- });
186
- }
187
- if (debug) console.log('*** dotenv ***', dotenv);
188
-
189
- // Execute post-hook.
190
- if (postHook) await postHook(options, dotenv);
191
-
192
- // Execute shell command.
193
- if (command) await (0, _execa.execaCommand)(command.replace(/ /g, '\\ '), {
194
- stdio: 'inherit',
195
- shell: true
196
- });
197
- });
198
- };
199
- exports.getCli = getCli;
@@ -1,13 +0,0 @@
1
- export const dotenvDefaults = {
2
- dotenvToken: '.env',
3
- excludeDynamic: false,
4
- excludeEnv: false,
5
- excludeGlobal: false,
6
- excludePrivate: false,
7
- excludePublic: false,
8
- loadProcess: false,
9
- log: false,
10
- logger: console.log,
11
- paths: ['./'],
12
- privateToken: 'local',
13
- };
package/lib/getCli.js DELETED
@@ -1,357 +0,0 @@
1
- // npm imports
2
- import { boolean } from 'boolean';
3
- import { Command, Option } from 'commander';
4
- import { execaCommand } from 'execa';
5
- import fromPairs from 'lodash.frompairs';
6
- import isString from 'lodash.isstring';
7
-
8
- // lib imports
9
- import { dotenvDefaults } from './dotenvDefaults.js';
10
- import { dotenvExpand } from './dotenvExpand.js';
11
- import { getDotenv } from './getDotenv.js';
12
-
13
- const booleanExpand = (value) => boolean(dotenvExpand(value));
14
-
15
- /**
16
- * GetDotenv CLI Options type
17
- *
18
- * @typedef {Object} GetDotenvCliOptions
19
- * @property {string} [cliInvocation] - cli invocation string (used for cli help)
20
- * @property {bool} [debug] - debug mode
21
- * @property {string} [defaultEnv] - default target environment
22
- * @property {string} [dotenvToken] - token indicating a dotenv file
23
- * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
24
- * @property {string} [env] - target environment
25
- * @property {bool} [excludeDynamic] - exclude dynamic variables
26
- * @property {bool} [excludeEnv] - exclude environment-specific variables
27
- * @property {bool} [excludeGlobal] - exclude global & dynamic variables
28
- * @property {bool} [excludePrivate] - exclude private variables
29
- * @property {bool} [excludePublic] - exclude public variables
30
- * @property {bool} [log] - log result to console
31
- * @property {function} [logger] - logger function
32
- * @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})
33
- * @property {string} [paths] - space-delimited list of input directory paths
34
- * @property {string} [privateToken] - token indicating private variables.
35
- * @property {bool} [suppressDotenv] - suppress dotenv loading
36
- */
37
-
38
- /**
39
- * GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.
40
- *
41
- * @async
42
- * @callback GetDotenvPreHookCallback
43
- * @param {GetDotenvCliOptions} options - inbound GetDotenv CLI Options object
44
- * @returns {GetDotenvCliOptions} transformed GetDotenv CLI Options object (undefined return value is ignored)
45
- */
46
-
47
- /**
48
- * GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.
49
- *
50
- * @async
51
- * @callback GetDotenvPostHookCallback
52
- * @param {GetDotenvCliOptions} options - GetDotenv CLI Options object
53
- * @param {object} dotenv - dotenv object
54
- */
55
-
56
- /**
57
- * GetDotenv CLI Config type
58
- *
59
- * @typedef {Object} GetDotenvCliConfig
60
- * @property {object} [config] - config options
61
- * @property {GetDotenvCliOptions} [config.defaultOptions] - default options
62
- * @property {GetDotenvPreHookCallback} [config.preHook] - transforms inbound options & executes side effects
63
- * @property {GetDotenvPostHookCallback} [config.postHook] - executes side effects within getdotenv context
64
- */
65
-
66
- /**
67
- * Generate a CLI for get-dotenv.
68
- *
69
- * @param {GetDotenvCliConfig} [config] - config object
70
- * @returns {object} The CLI command.
71
- */
72
- export const getCli = ({ defaultOptions = {}, preHook, postHook } = {}) => {
73
- let {
74
- cliInvocation = 'getdotenv',
75
- command,
76
- defaultEnv = 'dev',
77
- dotenvToken,
78
- dynamicPath,
79
- env,
80
- excludeDynamic,
81
- excludeEnv,
82
- excludeGlobal,
83
- excludePrivate,
84
- excludePublic,
85
- log,
86
- outputPath,
87
- paths,
88
- pathsDelimiter = '\\s+',
89
- privateToken,
90
- suppressDotenv,
91
- vars,
92
- varsAssignor = '=',
93
- varsDelimiter = '\\s+',
94
- } = {
95
- ...dotenvDefaults,
96
- ...defaultOptions,
97
- };
98
-
99
- if (Array.isArray(paths)) paths = paths.join(' ');
100
-
101
- return new Command()
102
- .name(cliInvocation)
103
- .description(
104
- 'Base CLI. All options except delimiters follow dotenv-expand rules.'
105
- )
106
- .enablePositionalOptions()
107
- .passThroughOptions()
108
- .option('-e, --env <string>', 'target environment', dotenvExpand, env)
109
- .option(
110
- '--default-env <string>',
111
- 'default target environment',
112
- dotenvExpand,
113
- defaultEnv
114
- )
115
- .option(
116
- '-p, --paths <string>',
117
- 'delimited list of paths to dotenv directory',
118
- dotenvExpand,
119
- paths
120
- )
121
- .option(
122
- '--paths-delimiter <string>',
123
- 'regex paths delimiter',
124
- pathsDelimiter
125
- )
126
- .option(
127
- '-v, --vars <string>',
128
- 'delimited list KEY=VALUE pairs',
129
- dotenvExpand,
130
- vars
131
- )
132
- .option('--vars-delimiter <string>', 'regex vars delimiter', varsDelimiter)
133
- .option(
134
- '--vars-assignor <string>',
135
- 'regex vars assignment operator',
136
- varsAssignor
137
- )
138
- .option(
139
- '-y, --dynamic-path <string>',
140
- 'dynamic variables path',
141
- dotenvExpand,
142
- dynamicPath
143
- )
144
- .option(
145
- '-o, --output-path <string>',
146
- 'consolidated output file, follows dotenv-expand rules using loaded env vars',
147
- dotenvExpand,
148
- outputPath
149
- )
150
- .addOption(
151
- new Option(
152
- '-n, --exclude-env',
153
- `exclude environment-specific variables${
154
- excludeEnv ? ' (default)' : ''
155
- }`
156
- ).conflicts('excludeEnvOff')
157
- )
158
- .addOption(
159
- new Option(
160
- '-N, --exclude-env-off',
161
- `exclude environment-specific variables OFF${
162
- !excludeEnv ? ' (default)' : ''
163
- }`
164
- ).conflicts('excludeEnv')
165
- )
166
- .addOption(
167
- new Option(
168
- '-g, --exclude-global',
169
- `exclude global variables${excludeGlobal ? ' (default)' : ''}`
170
- ).conflicts('excludeGlobalOff')
171
- )
172
- .addOption(
173
- new Option(
174
- '-G, --exclude-global-off',
175
- `exclude global variables OFF${!excludeGlobal ? ' (default)' : ''}`
176
- ).conflicts('excludeGlobal')
177
- )
178
- .addOption(
179
- new Option(
180
- '-r, --exclude-private',
181
- `exclude private variables${excludePrivate ? ' (default)' : ''}`
182
- ).conflicts('excludePrivateOff')
183
- )
184
- .addOption(
185
- new Option(
186
- '-R, --exclude-private-off',
187
- `exclude private variables OFF${!excludePrivate ? ' (default)' : ''}`
188
- ).conflicts('excludePrivate')
189
- )
190
- .addOption(
191
- new Option(
192
- '-u, --exclude-public',
193
- `exclude public variables${excludePublic ? ' (default)' : ''}`
194
- ).conflicts('excludePublicOff')
195
- )
196
- .addOption(
197
- new Option(
198
- '-U, --exclude-public-off',
199
- `exclude public variables OFF${!excludePublic ? ' (default)' : ''}`
200
- ).conflicts('excludePublic')
201
- )
202
- .addOption(
203
- new Option(
204
- '-z, --exclude-dynamic',
205
- `exclude dynamic variables${excludeDynamic ? ' (default)' : ''}`
206
- ).conflicts('excludeDynamicOff')
207
- )
208
- .addOption(
209
- new Option(
210
- '-Z, --exclude-dynamic-off',
211
- `exclude dynamic variables OFF${!excludeDynamic ? ' (default)' : ''}`
212
- ).conflicts('excludeDynamic')
213
- )
214
- .addOption(
215
- new Option(
216
- '-l, --log',
217
- `console log extracted variables${log ? ' (default)' : ''}`
218
- ).conflicts('logOff')
219
- )
220
- .addOption(
221
- new Option(
222
- '-L, --log-off',
223
- `console log extracted variables OFF${!log ? ' (default)' : ''}`
224
- ).conflicts('log')
225
- )
226
- .option(
227
- '-x, --suppress-dotenv',
228
- 'suppress dotenv loading',
229
- booleanExpand,
230
- suppressDotenv ?? false
231
- )
232
- .option(
233
- '-c, --command <string>',
234
- 'shell command string',
235
- dotenvExpand,
236
- command
237
- )
238
- .option(
239
- '--dotenv-token <string>',
240
- 'token indicating a dotenv file',
241
- dotenvExpand,
242
- dotenvToken
243
- )
244
- .option(
245
- '--private-token <string>',
246
- 'token indicating private variables',
247
- dotenvExpand,
248
- privateToken
249
- )
250
- .option('-D, --debug', 'debug mode')
251
- .addCommand(
252
- new Command()
253
- .name('cmd')
254
- .description('execute shell command string (default command)')
255
- .configureHelp({ showGlobalOptions: true })
256
- .enablePositionalOptions()
257
- .passThroughOptions()
258
- .action(async (options, { args }) => {
259
- if (args.length)
260
- await execaCommand(args.join('\\ '), {
261
- stdio: 'inherit',
262
- shell: true,
263
- });
264
- }),
265
- { isDefault: true }
266
- )
267
- .hook('preSubcommand', async (thisCommand) => {
268
- // Inherit options from parent command.
269
- const getdotenvOptions = process.env['getdotenvOptions']
270
- ? JSON.parse(process.env['getdotenvOptions'])
271
- : {};
272
-
273
- const {
274
- excludeDynamicOff,
275
- excludeEnvOff,
276
- excludeGlobalOff,
277
- excludePrivateOff,
278
- excludePublicOff,
279
- logOff,
280
- ...localOptions
281
- } = thisCommand.opts();
282
-
283
- if (excludeDynamicOff) localOptions.excludeDynamic = false;
284
- if (excludeEnvOff) localOptions.excludeEnv = false;
285
- if (excludeGlobalOff) localOptions.excludeGlobal = false;
286
- if (excludePrivateOff) localOptions.excludePrivate = false;
287
- if (excludePublicOff) localOptions.excludePublic = false;
288
- if (logOff) localOptions.log = false;
289
-
290
- let mergedOptions = {
291
- ...getdotenvOptions,
292
- ...localOptions,
293
- };
294
-
295
- // Execute pre-hook.
296
- const options = preHook
297
- ? (await preHook(mergedOptions)) ?? mergedOptions
298
- : mergedOptions;
299
-
300
- // Get options.
301
- let {
302
- command,
303
- debug,
304
- defaultEnv,
305
- env,
306
- paths,
307
- pathsDelimiter,
308
- suppressDotenv,
309
- vars: varsStr,
310
- varsDelimiter,
311
- varsAssignor,
312
- ...rest
313
- } = options;
314
-
315
- // Parse vars.
316
- const vars = varsStr?.length
317
- ? fromPairs(
318
- varsStr
319
- .split(new RegExp(varsDelimiter))
320
- .map((v) => v.split(new RegExp(varsAssignor)))
321
- )
322
- : undefined;
323
-
324
- if (debug) {
325
- console.log('*** getdotenvOptions ***', getdotenvOptions);
326
- console.log('*** localOptions ***', localOptions);
327
- console.log('*** mergedOptions ***', mergedOptions);
328
- console.log('*** options ***', options);
329
- console.log('*** vars ***', vars);
330
- }
331
-
332
- // Execute getdotenv.
333
- if (paths?.length && !suppressDotenv) {
334
- if (isString(paths)) paths = paths?.split(new RegExp(pathsDelimiter));
335
-
336
- var dotenv = await getDotenv({
337
- ...rest,
338
- env: env ?? defaultEnv,
339
- loadProcess: true,
340
- paths,
341
- vars,
342
- });
343
- }
344
-
345
- if (debug) console.log('*** dotenv ***', dotenv);
346
-
347
- // Execute post-hook.
348
- if (postHook) await postHook(options, dotenv);
349
-
350
- // Execute shell command.
351
- if (command)
352
- await execaCommand(command.replace(/ /g, '\\ '), {
353
- stdio: 'inherit',
354
- shell: true,
355
- });
356
- });
357
- };