|
1 | | -import { existsSync, readdirSync, readFileSync, statSync } from 'fs'; |
| 1 | +import { closeSync, existsSync, fstatSync, openSync, readdirSync, readFileSync } from 'fs'; |
2 | 2 | import { dirname, join, relative } from 'path'; |
3 | 3 | import { fileURLToPath } from 'url'; |
4 | 4 |
|
@@ -83,35 +83,47 @@ function readDirectoryRecursively( |
83 | 83 | return files; |
84 | 84 | } |
85 | 85 |
|
86 | | - const entries = readdirSync(dirPath); |
| 86 | + const entries = readdirSync(dirPath, { withFileTypes: true }); |
87 | 87 |
|
88 | 88 | for (const entry of entries) { |
89 | | - const fullPath = join(dirPath, entry); |
| 89 | + const entryName = entry.name; |
| 90 | + const fullPath = join(dirPath, entryName); |
90 | 91 | const relativePath = relative(basePath, fullPath); |
91 | 92 |
|
92 | 93 | // Skip excluded files |
93 | | - if (exclude.includes(entry) || exclude.includes(relativePath)) { |
| 94 | + if (exclude.includes(entryName) || exclude.includes(relativePath)) { |
94 | 95 | continue; |
95 | 96 | } |
96 | 97 |
|
97 | | - const stat = statSync(fullPath); |
98 | | - |
99 | | - if (stat.isDirectory()) { |
| 98 | + if (entry.isDirectory()) { |
100 | 99 | // Recursively read subdirectory |
101 | 100 | const subFiles = readDirectoryRecursively(fullPath, basePath, exclude); |
102 | 101 | subFiles.forEach((content, path) => files.set(path, content)); |
103 | | - } else if (stat.isFile()) { |
104 | | - // Read file content (skip binary files) |
105 | | - if (!isBinaryFile(fullPath)) { |
106 | | - try { |
107 | | - const content = readFileSync(fullPath, 'utf-8'); |
| 102 | + } else if (entry.isFile()) { |
| 103 | + let fd: number | null = null; |
| 104 | + |
| 105 | + try { |
| 106 | + fd = openSync(fullPath, 'r'); |
| 107 | + const stat = fstatSync(fd); |
| 108 | + |
| 109 | + if (!stat.isFile()) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + // Read file content (skip binary files) |
| 114 | + if (!isBinaryFile(fullPath)) { |
| 115 | + const content = readFileSync(fd, 'utf-8'); |
108 | 116 | files.set(relativePath, content); |
109 | | - } catch { |
110 | | - // Skip files that can't be read as text |
| 117 | + } else { |
| 118 | + // For binary files, store a marker to copy them |
| 119 | + files.set(relativePath, `__BINARY__:${fullPath}`); |
| 120 | + } |
| 121 | + } catch { |
| 122 | + // Skip files that can't be opened/read |
| 123 | + } finally { |
| 124 | + if (fd !== null) { |
| 125 | + closeSync(fd); |
111 | 126 | } |
112 | | - } else { |
113 | | - // For binary files, store a marker to copy them |
114 | | - files.set(relativePath, `__BINARY__:${fullPath}`); |
115 | 127 | } |
116 | 128 | } |
117 | 129 | } |
|
0 commit comments