From 466fba74355abb567598f205cc73a5d78c093918 Mon Sep 17 00:00:00 2001 From: NoBody Date: Wed, 2 Aug 2023 02:15:28 -0500 Subject: [PATCH] Update Hex Rexp and Rewrite Recursive Function --- package.json | 5 ++--- src/lib/files.ts | 36 ++++++++++++------------------------ src/lib/validations.ts | 2 +- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 1df0906..e86dd0d 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "taskwizard", - "version": "0.1.0", + "version": "0.1.1", "description": "A versatile package containing useful code snippets for simple tasks", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ - "dist", - "src" + "dist" ], "engines": { "node": ">=10" diff --git a/src/lib/files.ts b/src/lib/files.ts index e919a85..2e73626 100644 --- a/src/lib/files.ts +++ b/src/lib/files.ts @@ -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 { + 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 +} \ No newline at end of file diff --git a/src/lib/validations.ts b/src/lib/validations.ts index 869e9e9..fda8889 100644 --- a/src/lib/validations.ts +++ b/src/lib/validations.ts @@ -4,7 +4,7 @@ * @returns Returns true if the text is a valid hexadecimal color code, otherwise returns false. */ export function isHex(colorCode: string): boolean { - return /^#[0-9A-F]{6}$/i.test(colorCode); + return /^#?([a-f0-9]{6}|[a-f0-9]{3})$/.test(colorCode); } /**