@@ -18,11 +18,33 @@ const isManifest = (fileName) => fileName.endsWith(MANIFEST_SUFFIX);
1818const 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+
2143const 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) => {
4466const 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
6183const 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