Skip to content

Commit 9cd6a0e

Browse files
committed
fix(hooks): skip unreadable dirs in knowledge splitter
1 parent e67d310 commit 9cd6a0e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

scripts/split-knowledge-large-files.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,33 @@ const isManifest = (fileName) => fileName.endsWith(MANIFEST_SUFFIX);
1818
const getPartRegex = (baseName) =>
1919
new RegExp(`^${escapeRegExp(baseName)}\\.part\\d+$`);
2020

21+
const isSkippableReadError = (error) =>
22+
error &&
23+
typeof error === "object" &&
24+
"code" in error &&
25+
(error.code === "EACCES" ||
26+
error.code === "EPERM" ||
27+
error.code === "ENOENT");
28+
29+
const readDirEntries = (dir, withFileTypes) => {
30+
try {
31+
return fs.readdirSync(
32+
dir,
33+
withFileTypes ? { withFileTypes: true } : undefined
34+
);
35+
} catch (error) {
36+
if (isSkippableReadError(error)) {
37+
return [];
38+
}
39+
throw error;
40+
}
41+
};
42+
2143
const findKnowledgeRoots = (startDir) => {
2244
const result = [];
2345

2446
const visit = (dir) => {
25-
const entries = fs.readdirSync(dir, { withFileTypes: true });
47+
const entries = readDirEntries(dir, true);
2648
for (const entry of entries) {
2749
if (!entry.isDirectory()) continue;
2850
if (WALK_IGNORE_DIR_NAMES.has(entry.name)) continue;
@@ -44,7 +66,7 @@ const findKnowledgeRoots = (startDir) => {
4466
const removeSplitArtifacts = (filePath) => {
4567
const dir = path.dirname(filePath);
4668
const baseName = path.basename(filePath);
47-
const entries = fs.readdirSync(dir);
69+
const entries = readDirEntries(dir, false);
4870
const partRegex = getPartRegex(baseName);
4971

5072
for (const entry of entries) {
@@ -59,7 +81,7 @@ const removeSplitArtifacts = (filePath) => {
5981
};
6082

6183
const walk = (dir) => {
62-
const entries = fs.readdirSync(dir, { withFileTypes: true });
84+
const entries = readDirEntries(dir, true);
6385
for (const entry of entries) {
6486
const fullPath = path.join(dir, entry.name);
6587
if (entry.isDirectory()) {

0 commit comments

Comments
 (0)