@@ -5,13 +5,19 @@ import * as pathLib from 'path';
5
5
6
6
const camelCase = require ( 'lodash.camelcase' ) ;
7
7
8
+ // import args as a first internal module
9
+ import { args as argsLib , Args , ArgsOptions } from './args' ;
10
+ // parse args as a first thing; argsLib modifies global namespace
11
+ // therefore it is better to do it as a first thing to prevent bugs
12
+ // when modules use this global setting during their require phase
13
+ // TODO(code): remove once https://app.stepsize.com/issue/c2f6253e-7240-436f-943c-23a897558156/2-http-libraries-in-cli is solved
14
+ const globalArgs = argsLib ( process . argv ) ;
8
15
// assert supported node runtime version
9
16
import * as runtime from './runtime' ;
10
17
// require analytics as soon as possible to start measuring execution time
11
18
import * as analytics from '../lib/analytics' ;
12
19
import * as alerts from '../lib/alerts' ;
13
20
import * as sln from '../lib/sln' ;
14
- import { args as argsLib , Args , ArgsOptions } from './args' ;
15
21
import { TestCommandResult } from './commands/types' ;
16
22
import { copy } from './copy' ;
17
23
import spinner = require ( '../lib/spinner' ) ;
@@ -232,97 +238,103 @@ async function main() {
232
238
updateCheck ( ) ;
233
239
checkRuntime ( ) ;
234
240
235
- const args = argsLib ( process . argv ) ;
236
-
237
241
let res ;
238
242
let failed = false ;
239
243
let exitCode = EXIT_CODES . ERROR ;
240
244
try {
241
- modeValidation ( args ) ;
245
+ modeValidation ( globalArgs ) ;
242
246
// TODO: fix this, we do transformation to options and teh type doesn't reflect it
243
247
validateUnsupportedOptionCombinations (
244
- ( args . options as unknown ) as AllSupportedCliOptions ,
248
+ ( globalArgs . options as unknown ) as AllSupportedCliOptions ,
245
249
) ;
246
250
247
251
// IaC only: used for rolling out the experimental flow
248
252
// modify args if experimental flag not provided, based on feature flag
249
253
// this can be removed once experimental becomes the default
250
254
if (
251
- args . options [ 'iac' ] &&
252
- args . command === 'test' &&
253
- ! args . options [ 'experimental' ]
255
+ globalArgs . options [ 'iac' ] &&
256
+ globalArgs . command === 'test' &&
257
+ ! globalArgs . options [ 'experimental' ]
254
258
) {
255
259
const iacOrgSettings = await getIacOrgSettings ( ) ;
256
260
const experimentalFlowEnabled = await isFeatureFlagSupportedForOrg (
257
261
camelCase ( 'experimental-local-exec-iac' ) ,
258
262
iacOrgSettings . meta . org ,
259
263
) ;
260
- args . options [ 'experimental' ] = ! ! experimentalFlowEnabled . ok ;
264
+ globalArgs . options [ 'experimental' ] = ! ! experimentalFlowEnabled . ok ;
261
265
}
262
266
263
- if ( args . options [ 'app-vulns' ] && args . options [ 'json' ] ) {
267
+ if ( globalArgs . options [ 'app-vulns' ] && globalArgs . options [ 'json' ] ) {
264
268
throw new UnsupportedOptionCombinationError ( [
265
269
'Application vulnerabilities is currently not supported with JSON output. ' +
266
270
'Please try using —app-vulns only to get application vulnerabilities, or ' +
267
271
'—json only to get your image vulnerabilties, excluding the application ones.' ,
268
272
] ) ;
269
273
}
270
- if ( args . options [ 'group-issues' ] && args . options [ 'iac' ] ) {
274
+ if ( globalArgs . options [ 'group-issues' ] && globalArgs . options [ 'iac' ] ) {
271
275
throw new UnsupportedOptionCombinationError ( [
272
276
'--group-issues is currently not supported for Snyk IaC.' ,
273
277
] ) ;
274
278
}
275
279
if (
276
- args . options [ 'group-issues' ] &&
277
- ! args . options [ 'json' ] &&
278
- ! args . options [ 'json-file-output' ]
280
+ globalArgs . options [ 'group-issues' ] &&
281
+ ! globalArgs . options [ 'json' ] &&
282
+ ! globalArgs . options [ 'json-file-output' ]
279
283
) {
280
284
throw new UnsupportedOptionCombinationError ( [
281
285
'JSON output is required to use --group-issues, try adding --json.' ,
282
286
] ) ;
283
287
}
284
288
285
289
if (
286
- args . options . file &&
287
- typeof args . options . file === 'string' &&
288
- ( args . options . file as string ) . match ( / \. s l n $ / )
290
+ globalArgs . options . file &&
291
+ typeof globalArgs . options . file === 'string' &&
292
+ ( globalArgs . options . file as string ) . match ( / \. s l n $ / )
289
293
) {
290
- if ( args . options [ 'project-name' ] ) {
294
+ if ( globalArgs . options [ 'project-name' ] ) {
291
295
throw new UnsupportedOptionCombinationError ( [
292
296
'file=*.sln' ,
293
297
'project-name' ,
294
298
] ) ;
295
299
}
296
- sln . updateArgs ( args ) ;
297
- } else if ( typeof args . options . file === 'boolean' ) {
300
+ sln . updateArgs ( globalArgs ) ;
301
+ } else if ( typeof globalArgs . options . file === 'boolean' ) {
298
302
throw new FileFlagBadInputError ( ) ;
299
303
}
300
304
301
305
if (
302
- typeof args . options . detectionDepth !== 'undefined' &&
303
- ( args . options . detectionDepth <= 0 ||
304
- Number . isNaN ( args . options . detectionDepth ) )
306
+ typeof globalArgs . options . detectionDepth !== 'undefined' &&
307
+ ( globalArgs . options . detectionDepth <= 0 ||
308
+ Number . isNaN ( globalArgs . options . detectionDepth ) )
305
309
) {
306
310
throw new InvalidDetectionDepthValue ( ) ;
307
311
}
308
312
309
- validateUnsupportedSarifCombinations ( args ) ;
313
+ validateUnsupportedSarifCombinations ( globalArgs ) ;
310
314
311
- validateOutputFile ( args . options , 'json' , new JsonFileOutputBadInputError ( ) ) ;
312
- validateOutputFile ( args . options , 'sarif' , new SarifFileOutputEmptyError ( ) ) ;
315
+ validateOutputFile (
316
+ globalArgs . options ,
317
+ 'json' ,
318
+ new JsonFileOutputBadInputError ( ) ,
319
+ ) ;
320
+ validateOutputFile (
321
+ globalArgs . options ,
322
+ 'sarif' ,
323
+ new SarifFileOutputEmptyError ( ) ,
324
+ ) ;
313
325
314
- checkPaths ( args ) ;
326
+ checkPaths ( globalArgs ) ;
315
327
316
- res = await runCommand ( args ) ;
328
+ res = await runCommand ( globalArgs ) ;
317
329
} catch ( error ) {
318
330
failed = true ;
319
331
320
- const response = await handleError ( args , error ) ;
332
+ const response = await handleError ( globalArgs , error ) ;
321
333
res = response . res ;
322
334
exitCode = response . exitCode ;
323
335
}
324
336
325
- if ( ! args . options . json ) {
337
+ if ( ! globalArgs . options . json ) {
326
338
console . log ( alerts . displayAlerts ( ) ) ;
327
339
}
328
340
0 commit comments