/*
 * Copyright (c) 2018, 2026, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const path=require("path");const fs=require("fs");const fileSets={};function getConfigData(sourcePath){try{if(!fs.existsSync(sourcePath)){return}let text=fs.readFileSync(sourcePath,{encoding:"utf8",flag:"r"});return JSON.parse(text)}catch(error){console.log("Error: "+error.stack);process.exit(0)}}function scanForFiles(startPath,include){let{testFilesExtension,excludeFolders,fileTypes,excludeFiles,includeFiles,includeFolders}=global.configData;if(testFilesExtension){fileTypes=[testFilesExtension]}if(!includeFolders.length){include=true}const regex=new RegExp(`.(${fileTypes.map(i=>"."+i).join("|")})$`,"i");try{if(!fs.existsSync(startPath)){return}let files=fs.readdirSync(startPath);for(let i=0;i<files.length;i++){let filepath=path.join(startPath,files[i]);let stat=fs.lstatSync(filepath);let filename=files[i];if(stat.isDirectory()){if(includeFolders.length&&(includeFolders.includes(filepath)||includeFolders.includes(filename))||!includeFolders.length&&!excludeFolders.includes(filepath)&&!excludeFolders.includes(filename)){scanForFiles(filepath,true)}else if(!excludeFolders.length||!excludeFolders.includes(filepath)&&!excludeFolders.includes(filename)){scanForFiles(filepath,include)}}else{if(include&&(!excludeFiles.length&&(includeFiles.includes(filepath)||includeFiles.includes(filename))||!includeFiles.length&&!excludeFiles.includes(filepath)&&!excludeFiles.includes(filename))){let info=regex.exec(filepath);if(info&&info["index"]){if(fileSets[info[1]]){fileSets[info[1]].push(filepath)}else{fileSets[info[1]]=[filepath]}}}}}return fileSets}catch(error){console.log("Error: "+error.stack);process.exit(0)}}exports.scanForFiles=scanForFiles;exports.getConfigData=getConfigData;