snyk 1.807.0 → 1.808.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.
@@ -321,45 +321,49 @@ const errors_1 = __webpack_require__(55191);
321
321
  const error_utils_1 = __webpack_require__(23872);
322
322
  const DEFAULT_ENCODING = 'utf-8';
323
323
  async function loadFiles(pathToScan, options = {}) {
324
- let filePaths = [pathToScan];
325
- if (detect_1.isLocalFolder(pathToScan)) {
326
- filePaths = getFilePathsFromDirectory(pathToScan, {
327
- maxDepth: options.detectionDepth,
328
- });
324
+ const filePaths = getFilePathsFromDirectory(pathToScan, {
325
+ maxDepth: options.detectionDepth,
326
+ });
327
+ if (filePaths.length === 0) {
328
+ throw new NoFilesToScanError();
329
329
  }
330
- const filesToScan = [];
331
- for (const filePath of filePaths) {
330
+ const loadedFiles = await Promise.all(filePaths.map(async (filePath) => {
332
331
  try {
333
- const fileData = await tryLoadFileData(filePath);
334
- if (fileData) {
335
- filesToScan.push(fileData);
336
- }
332
+ return await tryLoadFileData(filePath);
337
333
  }
338
334
  catch (e) {
339
335
  throw new FailedToLoadFileError(filePath);
340
336
  }
341
- }
342
- if (filesToScan.length === 0) {
343
- throw new NoFilesToScanError();
344
- }
345
- return filesToScan.filter((file) => file.fileContent !== '');
337
+ }));
338
+ return loadedFiles.filter((file) => file.fileContent !== '');
346
339
  }
347
340
  exports.loadFiles = loadFiles;
341
+ function hasValidFileType(filePath) {
342
+ return types_1.VALID_FILE_TYPES.includes(iac_parser_1.getFileType(filePath));
343
+ }
348
344
  function getFilePathsFromDirectory(pathToScan, options = {}) {
349
- const directoryPaths = makeDirectoryIterator_1.makeDirectoryIterator(pathToScan, {
350
- maxDepth: options.maxDepth,
351
- });
352
- const directoryFilePaths = [];
353
- for (const filePath of directoryPaths) {
354
- directoryFilePaths.push(filePath);
345
+ const resFilePaths = [];
346
+ if (detect_1.isLocalFolder(pathToScan)) {
347
+ // Directory
348
+ const dirIterator = makeDirectoryIterator_1.makeDirectoryIterator(pathToScan, {
349
+ maxDepth: options.maxDepth,
350
+ });
351
+ for (const filePath of dirIterator) {
352
+ if (hasValidFileType(filePath)) {
353
+ resFilePaths.push(filePath);
354
+ }
355
+ }
355
356
  }
356
- return directoryFilePaths;
357
+ else {
358
+ // File
359
+ if (hasValidFileType(pathToScan)) {
360
+ resFilePaths.push(pathToScan);
361
+ }
362
+ }
363
+ return resFilePaths;
357
364
  }
358
365
  async function tryLoadFileData(pathToScan) {
359
366
  const fileType = iac_parser_1.getFileType(pathToScan);
360
- if (!types_1.VALID_FILE_TYPES.includes(fileType)) {
361
- return null;
362
- }
363
367
  const fileContent = (await fs_1.promises.readFile(pathToScan, DEFAULT_ENCODING)).toString();
364
368
  return {
365
369
  filePath: pathToScan,