@karmaniverous/get-dotenv 3.0.4 → 3.0.6
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 +31 -25
- package/bin/getdotenv/index.js +1 -1
- package/dist/default/lib/getCli.js +39 -20
- package/lib/getCli.js +251 -188
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Load environment variables with a cascade of environment-aware dotenv files. You
|
|
|
13
13
|
|
|
14
14
|
`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
15
|
|
|
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.
|
|
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. Any child `getdotenv` instances will inherit as defaults the parent shell's environment and optionally its `getdotenv` settings.
|
|
17
17
|
|
|
18
18
|
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
19
|
|
|
@@ -79,31 +79,37 @@ Usage: getdotenv [options] [command]
|
|
|
79
79
|
Base CLI. All options except delimiters follow dotenv-expand rules.
|
|
80
80
|
|
|
81
81
|
Options:
|
|
82
|
-
-e, --env <string>
|
|
83
|
-
--default-env <string>
|
|
84
|
-
-p, --paths <string>
|
|
85
|
-
--paths-delimiter <string>
|
|
86
|
-
-v, --vars <string>
|
|
87
|
-
--vars-delimiter <string>
|
|
88
|
-
--vars-assignor <string>
|
|
89
|
-
-y, --dynamic-path <string>
|
|
90
|
-
-o, --output-path <string>
|
|
91
|
-
-n, --exclude-env
|
|
92
|
-
-
|
|
93
|
-
-
|
|
94
|
-
-
|
|
95
|
-
-
|
|
96
|
-
-
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
-
|
|
100
|
-
--
|
|
101
|
-
|
|
102
|
-
-
|
|
82
|
+
-e, --env <string> target environment
|
|
83
|
+
--default-env <string> default target environment (default: "dev")
|
|
84
|
+
-p, --paths <string> delimited list of paths to dotenv directory (default: "./")
|
|
85
|
+
--paths-delimiter <string> regex paths delimiter (default: "\\s+")
|
|
86
|
+
-v, --vars <string> delimited list KEY=VALUE pairs
|
|
87
|
+
--vars-delimiter <string> regex vars delimiter (default: "\\s+")
|
|
88
|
+
--vars-assignor <string> regex vars assignment operator (default: "=")
|
|
89
|
+
-y, --dynamic-path <string> dynamic variables path
|
|
90
|
+
-o, --output-path <string> consolidated output file, follows dotenv-expand rules using loaded env vars
|
|
91
|
+
-n, --exclude-env exclude environment-specific variables
|
|
92
|
+
-N, --exclude-env-off exclude environment-specific variables OFF (default)
|
|
93
|
+
-g, --exclude-global exclude global variables
|
|
94
|
+
-G, --exclude-global-off exclude global variables OFF (default)
|
|
95
|
+
-r, --exclude-private exclude private variables
|
|
96
|
+
-R, --exclude-private-off exclude private variables OFF (default)
|
|
97
|
+
-u, --exclude-public exclude public variables
|
|
98
|
+
-U, --exclude-public-off exclude public variables OFF (default)
|
|
99
|
+
-z, --exclude-dynamic exclude dynamic variables
|
|
100
|
+
-Z, --exclude-dynamic-off exclude dynamic variables OFF (default)
|
|
101
|
+
-l, --log console log extracted variables
|
|
102
|
+
-L, --log-off console log extracted variables OFF (default)
|
|
103
|
+
-x, --suppress-dotenv suppress dotenv loading (default: false)
|
|
104
|
+
-c, --command <string> shell command string
|
|
105
|
+
--dotenv-token <string> token indicating a dotenv file (default: ".env")
|
|
106
|
+
--private-token <string> token indicating private variables (default: "local")
|
|
107
|
+
-D, --debug debug mode
|
|
108
|
+
-h, --help display help for command
|
|
103
109
|
|
|
104
110
|
Commands:
|
|
105
|
-
cmd
|
|
106
|
-
help [command]
|
|
111
|
+
cmd execute shell command string (default command)
|
|
112
|
+
help [command] display help for command
|
|
107
113
|
```
|
|
108
114
|
|
|
109
115
|
# API Documentation
|
|
@@ -194,6 +200,7 @@ GetDotenv CLI Options type
|
|
|
194
200
|
| Name | Type | Description |
|
|
195
201
|
| --- | --- | --- |
|
|
196
202
|
| [cliInvocation] | <code>string</code> | cli invocation string (used for cli help) |
|
|
203
|
+
| [debug] | <code>bool</code> | debug mode |
|
|
197
204
|
| [defaultEnv] | <code>string</code> | default target environment |
|
|
198
205
|
| [dotenvToken] | <code>string</code> | token indicating a dotenv file |
|
|
199
206
|
| [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
|
|
@@ -208,7 +215,6 @@ GetDotenv CLI Options type
|
|
|
208
215
|
| [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)) |
|
|
209
216
|
| [paths] | <code>string</code> | space-delimited list of input directory paths |
|
|
210
217
|
| [privateToken] | <code>string</code> | token indicating private variables. |
|
|
211
|
-
| [shell] | <code>bool</code> \| <code>string</code> | execa shell option |
|
|
212
218
|
| [suppressDotenv] | <code>bool</code> | suppress dotenv loading |
|
|
213
219
|
|
|
214
220
|
<a name="GetDotenvPreHookCallback"></a>
|
package/bin/getdotenv/index.js
CHANGED
|
@@ -24,6 +24,7 @@ const booleanExpand = value => (0, _boolean.boolean)((0, _dotenvExpand.dotenvExp
|
|
|
24
24
|
*
|
|
25
25
|
* @typedef {Object} GetDotenvCliOptions
|
|
26
26
|
* @property {string} [cliInvocation] - cli invocation string (used for cli help)
|
|
27
|
+
* @property {bool} [debug] - debug mode
|
|
27
28
|
* @property {string} [defaultEnv] - default target environment
|
|
28
29
|
* @property {string} [dotenvToken] - token indicating a dotenv file
|
|
29
30
|
* @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
@@ -38,7 +39,6 @@ const booleanExpand = value => (0, _boolean.boolean)((0, _dotenvExpand.dotenvExp
|
|
|
38
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})
|
|
39
40
|
* @property {string} [paths] - space-delimited list of input directory paths
|
|
40
41
|
* @property {string} [privateToken] - token indicating private variables.
|
|
41
|
-
* @property {bool|string} [shell] - execa shell option
|
|
42
42
|
* @property {bool} [suppressDotenv] - suppress dotenv loading
|
|
43
43
|
*/
|
|
44
44
|
|
|
@@ -99,7 +99,6 @@ const getCli = function () {
|
|
|
99
99
|
paths,
|
|
100
100
|
pathsDelimiter = '\\s+',
|
|
101
101
|
privateToken,
|
|
102
|
-
shell,
|
|
103
102
|
suppressDotenv,
|
|
104
103
|
vars,
|
|
105
104
|
varsAssignor = '=',
|
|
@@ -109,37 +108,49 @@ const getCli = function () {
|
|
|
109
108
|
...defaultOptions
|
|
110
109
|
};
|
|
111
110
|
if (Array.isArray(paths)) paths = paths.join(' ');
|
|
112
|
-
return new _commander.Command().name(cliInvocation)
|
|
113
|
-
// .usage('[options] [command] [command options] [commad args]')
|
|
114
|
-
.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).option('-n, --exclude-env [bool]', 'exclude environment-specific variables', booleanExpand, excludeEnv ?? false).option('-g, --exclude-global [bool]', 'exclude global & dynamic variables', booleanExpand, excludeGlobal ?? false).option('-r, --exclude-private [bool]', 'exclude private variables', booleanExpand, excludePrivate ?? false).option('-u, --exclude-public [bool]', 'exclude public variables', booleanExpand, excludePublic ?? false).option('-z, --exclude-dynamic [bool]', 'exclude dynamic variables', booleanExpand, excludeDynamic ?? false).option('-l, --log [bool]', 'console log extracted variables', booleanExpand, log ?? 'false').option('-x, --suppress-dotenv', 'suppress dotenv loading', booleanExpand, suppressDotenv ?? false).option('-c, --command <string>', 'shell command string', _dotenvExpand.dotenvExpand, command).option('-s, --shell <string>', 'execa shell option', _dotenvExpand.dotenvExpand, shell).option('--dotenv-token <string>', 'token indicating a dotenv file', _dotenvExpand.dotenvExpand, dotenvToken).option('--private-token <string>', 'token indicating private variables', _dotenvExpand.dotenvExpand, privateToken).addCommand(new _commander.Command().name('cmd').description('execute shell command string')
|
|
115
|
-
// .description('execute shell command string (default command)')
|
|
116
|
-
.configureHelp({
|
|
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({
|
|
117
112
|
showGlobalOptions: true
|
|
118
113
|
}).enablePositionalOptions().passThroughOptions().action(async (options, _ref) => {
|
|
119
114
|
let {
|
|
120
|
-
args
|
|
121
|
-
parent
|
|
115
|
+
args
|
|
122
116
|
} = _ref;
|
|
123
|
-
if (args.length) await (0, _execa.execaCommand)(args.join(' '), {
|
|
117
|
+
if (args.length) await (0, _execa.execaCommand)(args.join('\\ '), {
|
|
124
118
|
stdio: 'inherit',
|
|
125
|
-
shell:
|
|
119
|
+
shell: true
|
|
126
120
|
});
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
).hook('preSubcommand', async thisCommand => {
|
|
121
|
+
}), {
|
|
122
|
+
isDefault: true
|
|
123
|
+
}).hook('preSubcommand', async thisCommand => {
|
|
130
124
|
var _paths;
|
|
131
125
|
// Inherit options from parent command.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
|
135
145
|
};
|
|
136
146
|
|
|
137
147
|
// Execute pre-hook.
|
|
138
|
-
|
|
148
|
+
const options = preHook ? (await preHook(mergedOptions)) ?? mergedOptions : mergedOptions;
|
|
139
149
|
|
|
140
150
|
// Get options.
|
|
141
151
|
let {
|
|
142
152
|
command,
|
|
153
|
+
debug,
|
|
143
154
|
defaultEnv,
|
|
144
155
|
env,
|
|
145
156
|
paths,
|
|
@@ -153,6 +164,13 @@ const getCli = function () {
|
|
|
153
164
|
|
|
154
165
|
// Parse vars.
|
|
155
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
|
+
}
|
|
156
174
|
|
|
157
175
|
// Execute getdotenv.
|
|
158
176
|
if ((_paths = paths) !== null && _paths !== void 0 && _paths.length && !suppressDotenv) {
|
|
@@ -166,14 +184,15 @@ const getCli = function () {
|
|
|
166
184
|
vars
|
|
167
185
|
});
|
|
168
186
|
}
|
|
187
|
+
if (debug) console.log('*** dotenv ***', dotenv);
|
|
169
188
|
|
|
170
189
|
// Execute post-hook.
|
|
171
190
|
if (postHook) await postHook(options, dotenv);
|
|
172
191
|
|
|
173
192
|
// Execute shell command.
|
|
174
|
-
if (command) await (0, _execa.execaCommand)(command, {
|
|
193
|
+
if (command) await (0, _execa.execaCommand)(command.replace(/ /g, '\\ '), {
|
|
175
194
|
stdio: 'inherit',
|
|
176
|
-
shell
|
|
195
|
+
shell: true
|
|
177
196
|
});
|
|
178
197
|
});
|
|
179
198
|
};
|
package/lib/getCli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// npm imports
|
|
2
2
|
import { boolean } from 'boolean';
|
|
3
|
-
import { Command } from 'commander';
|
|
3
|
+
import { Command, Option } from 'commander';
|
|
4
4
|
import { execaCommand } from 'execa';
|
|
5
5
|
import fromPairs from 'lodash.frompairs';
|
|
6
6
|
import isString from 'lodash.isstring';
|
|
@@ -17,6 +17,7 @@ const booleanExpand = (value) => boolean(dotenvExpand(value));
|
|
|
17
17
|
*
|
|
18
18
|
* @typedef {Object} GetDotenvCliOptions
|
|
19
19
|
* @property {string} [cliInvocation] - cli invocation string (used for cli help)
|
|
20
|
+
* @property {bool} [debug] - debug mode
|
|
20
21
|
* @property {string} [defaultEnv] - default target environment
|
|
21
22
|
* @property {string} [dotenvToken] - token indicating a dotenv file
|
|
22
23
|
* @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
@@ -31,7 +32,6 @@ const booleanExpand = (value) => boolean(dotenvExpand(value));
|
|
|
31
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})
|
|
32
33
|
* @property {string} [paths] - space-delimited list of input directory paths
|
|
33
34
|
* @property {string} [privateToken] - token indicating private variables.
|
|
34
|
-
* @property {bool|string} [shell] - execa shell option
|
|
35
35
|
* @property {bool} [suppressDotenv] - suppress dotenv loading
|
|
36
36
|
*/
|
|
37
37
|
|
|
@@ -87,7 +87,6 @@ export const getCli = ({ defaultOptions = {}, preHook, postHook } = {}) => {
|
|
|
87
87
|
paths,
|
|
88
88
|
pathsDelimiter = '\\s+',
|
|
89
89
|
privateToken,
|
|
90
|
-
shell,
|
|
91
90
|
suppressDotenv,
|
|
92
91
|
vars,
|
|
93
92
|
varsAssignor = '=',
|
|
@@ -99,196 +98,260 @@ export const getCli = ({ defaultOptions = {}, preHook, postHook } = {}) => {
|
|
|
99
98
|
|
|
100
99
|
if (Array.isArray(paths)) paths = paths.join(' ');
|
|
101
100
|
|
|
102
|
-
return (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
'
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
'
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
'
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
'
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
'
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
'
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
'
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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
|
+
: {};
|
|
243
272
|
|
|
244
|
-
|
|
245
|
-
|
|
273
|
+
const {
|
|
274
|
+
excludeDynamicOff,
|
|
275
|
+
excludeEnvOff,
|
|
276
|
+
excludeGlobalOff,
|
|
277
|
+
excludePrivateOff,
|
|
278
|
+
excludePublicOff,
|
|
279
|
+
logOff,
|
|
280
|
+
...localOptions
|
|
281
|
+
} = thisCommand.opts();
|
|
246
282
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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;
|
|
260
314
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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;
|
|
269
323
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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
|
+
}
|
|
273
344
|
|
|
274
|
-
|
|
275
|
-
...rest,
|
|
276
|
-
env: env ?? defaultEnv,
|
|
277
|
-
loadProcess: true,
|
|
278
|
-
paths,
|
|
279
|
-
vars,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
345
|
+
if (debug) console.log('*** dotenv ***', dotenv);
|
|
282
346
|
|
|
283
|
-
|
|
284
|
-
|
|
347
|
+
// Execute post-hook.
|
|
348
|
+
if (postHook) await postHook(options, dotenv);
|
|
285
349
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
);
|
|
350
|
+
// Execute shell command.
|
|
351
|
+
if (command)
|
|
352
|
+
await execaCommand(command.replace(/ /g, '\\ '), {
|
|
353
|
+
stdio: 'inherit',
|
|
354
|
+
shell: true,
|
|
355
|
+
});
|
|
356
|
+
});
|
|
294
357
|
};
|