Skip to content

Commit b2262e0

Browse files
committed
Update findFiles function performance
1 parent e6a82c7 commit b2262e0

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/fileProcessors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function processFiles<T extends ExtractedData>(
2525
): T[] {
2626
return files
2727
.map((file) => {
28-
const content = readFileContent(file.path);
2928
try {
29+
const content = readFileContent(file.path);
3030
const extractedData = extractorFn(content);
3131
return {
3232
data: dataProcessor(extractedData),

src/utils.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path';
77
* @param isDebugEnabled - Whether debugging is enabled
88
*/
99
export function log(message: string, isDebugEnabled: boolean): void {
10-
if (isDebugEnabled) return;
10+
if (!isDebugEnabled) return;
1111
console.info(message);
1212
}
1313

@@ -19,7 +19,8 @@ export function log(message: string, isDebugEnabled: boolean): void {
1919
export function isValidClassName(className: string): boolean {
2020
// Class names must start with a letter, underscore, or hyphen
2121
// and can be followed by letters, numbers, underscores, or hyphens
22-
return /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/.test(className);
22+
const CLASS_NAME_REGEX = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/;
23+
return CLASS_NAME_REGEX.test(className);
2324
}
2425

2526
interface FileInfo {
@@ -35,17 +36,21 @@ interface FileInfo {
3536
*/
3637
export function findFiles(dir: string, pattern: RegExp): FileInfo[] {
3738
const files: FileInfo[] = [];
38-
const findFilesRecursive = (currentDir: string) => {
39-
const dirFiles = fs.readdirSync(currentDir);
40-
dirFiles.forEach((file) => {
41-
const fullPath = path.join(currentDir, file);
42-
if (fs.statSync(fullPath).isDirectory()) {
39+
40+
function findFilesRecursive(currentDir: string): void {
41+
const dirEntries = fs.readdirSync(currentDir, { withFileTypes: true });
42+
43+
for (const entry of dirEntries) {
44+
const fullPath = path.join(currentDir, entry.name);
45+
46+
if (entry.isDirectory()) {
4347
findFilesRecursive(fullPath);
44-
} else if (pattern.test(file)) {
45-
files.push({ name: file, path: fullPath });
48+
} else if (pattern.test(entry.name)) {
49+
files.push({ name: entry.name, path: fullPath });
4650
}
47-
});
48-
};
51+
}
52+
}
53+
4954
findFilesRecursive(dir);
5055
return files;
5156
}

0 commit comments

Comments
 (0)