-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Hex Rexp and Rewrite Recursive Function
- Loading branch information
Showing
3 changed files
with
15 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
import { lstatSync, readdirSync } from "fs"; | ||
import { extname, join } from "path"; | ||
import { join } from "path"; | ||
import { readdir } from 'node:fs/promises'; | ||
|
||
/** | ||
* Recursively search for files with specified extensions in a folder and its subfolders. | ||
* Recursively search for files in a folder. | ||
* | ||
* @param {string} folderPath - The path to the folder where the search should start. | ||
* @param {string[]} [allowedExtensions=[".js"]] - An array of allowed file extensions to include in the search. Default: [".js"] | ||
* @param {string} path - The path to the folder where the search should start. | ||
* @returns {string[]} - An array containing the absolute file paths of the found files. | ||
*/ | ||
export function searchFilesRecursive(folderPath: string, allowedExtensions: string[] = [".js"]): string[] { | ||
const filePaths: string[] = []; | ||
const readFilesRecursively = (directory: string) => { | ||
const files = readdirSync(join(process.cwd(), directory)); | ||
files.forEach((file) => { | ||
const stat = lstatSync(join(process.cwd(), directory, file)); | ||
if (stat.isDirectory()) { | ||
readFilesRecursively(join(directory, file)); | ||
} else { | ||
const extension = extname(file); | ||
if (!allowedExtensions.includes(extension)) return; | ||
const filePath = join(process.cwd(), directory, file); | ||
filePaths.push(`${filePath}`); | ||
} | ||
}); | ||
}; | ||
readFilesRecursively(folderPath); | ||
return filePaths; | ||
} | ||
export async function searchFilesRecursive(path: string): Promise<string[]> { | ||
const paths: string[] = []; | ||
for (const file of (await readdir(path, { withFileTypes: true }))) { | ||
const dir = join(path, file.name); | ||
paths.push(...(file.isDirectory() ? await searchFilesRecursive(dir) : [dir])); | ||
} | ||
return paths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters