@@ -159,6 +159,7 @@ class ChildrenCache extends lru_cache_1.LRUCache {
159159 }
160160}
161161exports . ChildrenCache = ChildrenCache ;
162+ const setAsCwd = Symbol ( 'PathScurry setAsCwd' ) ;
162163/**
163164 * Path objects are sort of like a super-powered
164165 * {@link https://nodejs.org/docs/latest/api/fs.html#class-fsdirent fs.Dirent}
@@ -291,6 +292,16 @@ class PathBase {
291292 #children;
292293 #linkTarget;
293294 #realpath;
295+ /**
296+ * This property is for compatibility with the Dirent class as of
297+ * Node v20, where Dirent['path'] refers to the path of the directory
298+ * that was passed to readdir. So, somewhat counterintuitively, this
299+ * property refers to the *parent* path, not the path object itself.
300+ * For root entries, it's the path to the entry itself.
301+ */
302+ get path ( ) {
303+ return ( this . parent || this ) . fullpath ( ) ;
304+ }
294305 /**
295306 * Do not create new Path objects directly. They should always be accessed
296307 * via the PathScurry class or other methods on the Path class.
@@ -438,8 +449,7 @@ class PathBase {
438449 return ( this . #relative = this . name ) ;
439450 }
440451 const pv = p . relative ( ) ;
441- const rp = pv + ( ! pv || ! p . parent ? '' : this . sep ) + name ;
442- return ( this . #relative = rp ) ;
452+ return pv + ( ! pv || ! p . parent ? '' : this . sep ) + name ;
443453 }
444454 /**
445455 * The relative path from the cwd, using / as the path separator.
@@ -458,8 +468,7 @@ class PathBase {
458468 return ( this . #relativePosix = this . fullpathPosix ( ) ) ;
459469 }
460470 const pv = p . relativePosix ( ) ;
461- const rp = pv + ( ! pv || ! p . parent ? '' : '/' ) + name ;
462- return ( this . #relativePosix = rp ) ;
471+ return pv + ( ! pv || ! p . parent ? '' : '/' ) + name ;
463472 }
464473 /**
465474 * The fully resolved path string for this Path entry
@@ -1111,6 +1120,33 @@ class PathBase {
11111120 this . #markENOREALPATH( ) ;
11121121 }
11131122 }
1123+ /**
1124+ * Internal method to mark this Path object as the scurry cwd,
1125+ * called by {@link PathScurry#chdir}
1126+ *
1127+ * @internal
1128+ */
1129+ [ setAsCwd ] ( oldCwd ) {
1130+ if ( oldCwd === this )
1131+ return ;
1132+ const changed = new Set ( [ ] ) ;
1133+ let rp = [ ] ;
1134+ let p = this ;
1135+ while ( p && p . parent ) {
1136+ changed . add ( p ) ;
1137+ p . #relative = rp . join ( this . sep ) ;
1138+ p . #relativePosix = rp . join ( '/' ) ;
1139+ p = p . parent ;
1140+ rp . push ( '..' ) ;
1141+ }
1142+ // now un-memoize parents of old cwd
1143+ p = oldCwd ;
1144+ while ( p && p . parent && ! changed . has ( p ) ) {
1145+ p . #relative = undefined ;
1146+ p . #relativePosix = undefined ;
1147+ p = p . parent ;
1148+ }
1149+ }
11141150}
11151151exports . PathBase = PathBase ;
11161152/**
@@ -1838,6 +1874,11 @@ class PathScurryBase {
18381874 process ( ) ;
18391875 return results ;
18401876 }
1877+ chdir ( path = this . cwd ) {
1878+ const oldCwd = this . cwd ;
1879+ this . cwd = typeof path === 'string' ? this . cwd . resolve ( path ) : path ;
1880+ this . cwd [ setAsCwd ] ( oldCwd ) ;
1881+ }
18411882}
18421883exports . PathScurryBase = PathScurryBase ;
18431884/**
0 commit comments