snyk 1.877.0 → 1.878.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.
@@ -511,7 +511,6 @@ exports.isIgnoredFile = isIgnoredFile;
511
511
 
512
512
  Object.defineProperty(exports, "__esModule", ({ value: true }));
513
513
  exports.getTerraformFilesInDirectoryGenerator = exports.getFilesForDirectory = exports.getAllDirectoriesForPath = exports.loadAndParseTerraformFiles = void 0;
514
- const fs = __webpack_require__(35747);
515
514
  const file_loader_1 = __webpack_require__(13552);
516
515
  const path = __webpack_require__(85622);
517
516
  const file_parser_1 = __webpack_require__(2314);
@@ -521,45 +520,25 @@ const handle_terraform_files_utils_1 = __webpack_require__(89708);
521
520
  * loading them, sending them to the parser and getting the parsing results back.
522
521
  * Then it concatenates and returns the new parsing results with the existing parsing results.
523
522
  * @param pathToScan - the path to scan provided by the user
524
- * @param parsedFiles - an array that holds all the existing parsedFiles
525
- * @param failedFiles - an array that holds all the existing failedFiles
526
523
  * @param maxDepth? - an optional maxDepth of directories if provided by the detection-depth flag
527
- * @returns { allParsedFiles, allFailedFiles} - all the parsing results so far
524
+ * @returns { parsedFiles, failedFiles} - all the parsing results so far
528
525
  */
529
- async function loadAndParseTerraformFiles(pathToScan, parsedFiles, failedFiles, maxDepth) {
530
- let allParsedFiles = parsedFiles, allFailedFiles = failedFiles;
526
+ async function loadAndParseTerraformFiles(pathToScan, maxDepth) {
527
+ let parsedFiles = [], failedFiles = [];
531
528
  const allDirectories = getAllDirectoriesForPath(pathToScan, maxDepth);
532
529
  // we load and parse files directory by directory
533
530
  // this is because we need all files in the same directory to share the same variable context
534
531
  for (const currentDirectory of allDirectories) {
535
532
  const filePathsInDirectory = getFilesForDirectory(pathToScan, currentDirectory);
536
- if (filePathsInDirectory.length === 0 || // skip directories that have 0 files but have other directories
537
- (filePathsInDirectory.length === 1 && allParsedFiles.length === 1) // skip single files that have already been parsed
538
- )
539
- continue;
540
- if (filePathsInDirectory.length === 1 &&
541
- allParsedFiles.length === 0 &&
542
- (!handle_terraform_files_utils_1.shouldBeParsed(filePathsInDirectory[0]) ||
543
- handle_terraform_files_utils_1.isIgnoredFile(filePathsInDirectory[0]))) {
544
- throw new file_loader_1.NoFilesToScanError();
545
- }
546
- try {
547
- const tfFilesToParse = await file_loader_1.loadContentForFiles(filePathsInDirectory);
548
- const { parsedFiles: parsedTfFiles, failedFiles: failedTfFiles, } = file_parser_1.parseTerraformFiles(tfFilesToParse);
549
- allParsedFiles = allParsedFiles.concat(parsedTfFiles);
550
- allFailedFiles = allFailedFiles.concat(failedTfFiles);
551
- }
552
- catch (err) {
553
- if (allParsedFiles.length !== 0 && err instanceof file_loader_1.NoFilesToScanError) {
554
- // ignore this error since we might only have TF files in the folder and we have separated them,
555
- // or if we have a single file scan of a non-TF file, and we have already parsed it
556
- }
557
- else {
558
- throw err;
559
- }
560
- }
533
+ const tfFilesToParse = await file_loader_1.loadContentForFiles(filePathsInDirectory);
534
+ const { parsedFiles: parsedTfFiles, failedFiles: failedTfFiles, } = file_parser_1.parseTerraformFiles(tfFilesToParse);
535
+ parsedFiles = parsedFiles.concat(parsedTfFiles);
536
+ failedFiles = failedFiles.concat(failedTfFiles);
561
537
  }
562
- return { allParsedFiles, allFailedFiles };
538
+ if (parsedFiles.length === 0) {
539
+ throw new file_loader_1.NoFilesToScanError();
540
+ }
541
+ return { parsedFiles, failedFiles };
563
542
  }
564
543
  exports.loadAndParseTerraformFiles = loadAndParseTerraformFiles;
565
544
  /**
@@ -573,10 +552,6 @@ function getAllDirectoriesForPath(pathToScan, maxDepth) {
573
552
  if (handle_terraform_files_utils_1.isSingleFile(pathToScan)) {
574
553
  return [path.resolve(pathToScan)];
575
554
  }
576
- // if the path we scanned itself is an empty directory, finish the scan here
577
- if (fs.readdirSync(pathToScan).length === 0) {
578
- throw new file_loader_1.NoFilesToScanError();
579
- }
580
555
  return [...getAllDirectoriesForPathGenerator(pathToScan, maxDepth)];
581
556
  }
582
557
  exports.getAllDirectoriesForPath = getAllDirectoriesForPath;
@@ -593,14 +568,17 @@ function* getAllDirectoriesForPathGenerator(pathToScan, maxDepth) {
593
568
  }
594
569
  }
595
570
  /**
596
- * Gets all file paths for the specific directory
571
+ * Gets all file paths for the specific directory. If the provided path is a supported file, then it gets returned.
597
572
  * @param pathToScan - the path to scan provided by the user
598
573
  * @param currentDirectory - the directory which we want to return files for
599
574
  * @returns {string[]} An array with all the Terraform filePaths for this directory
600
575
  */
601
576
  function getFilesForDirectory(pathToScan, currentDirectory) {
602
577
  if (handle_terraform_files_utils_1.isSingleFile(pathToScan)) {
603
- return [pathToScan];
578
+ if (handle_terraform_files_utils_1.shouldBeParsed(pathToScan) && !handle_terraform_files_utils_1.isIgnoredFile(pathToScan)) {
579
+ return [pathToScan];
580
+ }
581
+ return [];
604
582
  }
605
583
  else {
606
584
  return [...getTerraformFilesInDirectoryGenerator(currentDirectory)];
@@ -679,7 +657,7 @@ async function test(pathToScan, options) {
679
657
  if (validFileTypes &&
680
658
  !validFileTypes.includes(types_1.ValidFileType.Terraform) &&
681
659
  err instanceof file_loader_1.NoFilesToScanError) {
682
- // ignore this error since we might only have .tf files in the folder and we have separated them
660
+ // ignore this error since we might only have .tf files in the folder and we will parse them in the next block
683
661
  }
684
662
  else {
685
663
  throw err;
@@ -688,9 +666,19 @@ async function test(pathToScan, options) {
688
666
  // we may have loaded and parsed all but terraform files in the previous step
689
667
  // so now we check if we need to do a second load and parse which dereferences TF vars
690
668
  if (validFileTypes && !validFileTypes.includes(types_1.ValidFileType.Terraform)) {
691
- const { allParsedFiles, allFailedFiles, } = await measurable_methods_1.loadAndParseTerraformFiles(pathToScan, parsedFiles, failedFiles, options.detectionDepth);
692
- parsedFiles = allParsedFiles;
693
- failedFiles = allFailedFiles;
669
+ try {
670
+ const { parsedFiles: tfParsedFiles, failedFiles: tfFailedFiles, } = await measurable_methods_1.loadAndParseTerraformFiles(pathToScan, options.detectionDepth);
671
+ parsedFiles = parsedFiles.concat(tfParsedFiles);
672
+ failedFiles = failedFiles.concat(tfFailedFiles);
673
+ }
674
+ catch (err) {
675
+ if (parsedFiles.length > 0 && err instanceof file_loader_1.NoFilesToScanError) {
676
+ // ignore this error since we've discovered non-terraform files in the previous block
677
+ }
678
+ else {
679
+ throw err;
680
+ }
681
+ }
694
682
  }
695
683
  // Duplicate all the files and run them through the custom engine.
696
684
  if (rulesOrigin !== types_1.RulesOrigin.Internal) {