@@ -134,36 +134,29 @@ This flag is required in non-interactive terminal environments, such as a CI env
134
134
options ?: Input < TFlags , TGlobalFlags , TArgs > ,
135
135
argv ?: string [ ] ,
136
136
) : Promise < ParserOutput < TFlags , TGlobalFlags , TArgs > > {
137
- // If no environment is specified, don't modify the results
138
137
const flags = originalResult . flags as EnvironmentFlags
139
138
const environmentsFileName = this . environmentsFilename ( )
140
- let environment
141
- let isDefaultEnvironment = false
142
139
143
140
if ( ! environmentsFileName ) return originalResult
144
141
145
142
const environmentFileExists = await environmentFilePath ( environmentsFileName , { from : flags . path } )
146
- const environmentSpecified = flags . environment && flags . environment . length > 0
143
+ const environments = flags . environment ?? [ ]
144
+ const environmentSpecified = environments . length > 0
147
145
146
+ // Noop if no environment file exists and none was specified
148
147
if ( ! environmentFileExists && ! environmentSpecified ) return originalResult
149
148
150
- if ( ! environmentSpecified ) {
151
- // If no environment is specified attempt to load the default environment
152
- environment = await loadEnvironment ( 'default' , environmentsFileName , { from : flags . path } )
153
- isDefaultEnvironment = true
154
- }
155
-
156
- if ( ! flags . environment && ! environment ) return originalResult
157
-
158
- // If users pass multiple environments, do not load them and let each command handle it
159
- if ( flags . environment && flags . environment . length > 1 ) return originalResult
149
+ // Noop if multiple environments were specified (let commands handle this)
150
+ if ( environmentSpecified && environments . length > 1 ) return originalResult
160
151
161
- // If the specified environment isn't found, don't modify the results
162
- if ( ! environment && flags . environment ) {
163
- environment = await loadEnvironment ( flags . environment [ 0 ] as string , environmentsFileName , { from : flags . path } )
164
- }
152
+ const { environment, isDefaultEnvironment} = await this . loadEnvironmentForCommand (
153
+ flags . path ,
154
+ environmentsFileName ,
155
+ environments [ 0 ] ,
156
+ )
165
157
166
158
if ( ! environment ) return originalResult
159
+
167
160
// Parse using noDefaultsOptions to derive a list of flags specified as
168
161
// command-line arguments.
169
162
const noDefaultsResult = await super . parse < TFlags , TGlobalFlags , TArgs > ( noDefaultsOptions ( options ) , argv )
@@ -188,6 +181,24 @@ This flag is required in non-interactive terminal environments, such as a CI env
188
181
189
182
return result
190
183
}
184
+
185
+ /**
186
+ * Tries to load an environment to forward to the command. If no environment
187
+ * is specified it will try to load a default environment.
188
+ */
189
+ private async loadEnvironmentForCommand (
190
+ path : string | undefined ,
191
+ environmentsFileName : string ,
192
+ specifiedEnvironment : string | undefined ,
193
+ ) : Promise < { environment : JsonMap | undefined ; isDefaultEnvironment : boolean } > {
194
+ if ( specifiedEnvironment ) {
195
+ const environment = await loadEnvironment ( specifiedEnvironment , environmentsFileName , { from : path } )
196
+ return { environment, isDefaultEnvironment : false }
197
+ }
198
+
199
+ const environment = await loadEnvironment ( 'default' , environmentsFileName , { from : path } )
200
+ return { environment, isDefaultEnvironment : true }
201
+ }
191
202
}
192
203
193
204
export async function addFromParsedFlags ( flags : { path ?: string ; verbose ?: boolean } ) : Promise < void > {
0 commit comments