Nikolay Vitkov | 95ea045 | 2025-04-30 15:52:34 | [diff] [blame] | 1 | # path-scurry |
| 2 | |
| 3 | Extremely high performant utility for building tools that read |
| 4 | the file system, minimizing filesystem and path string munging |
| 5 | operations to the greatest degree possible. |
| 6 | |
| 7 | ## Ugh, yet another file traversal thing on npm? |
| 8 | |
| 9 | Yes. None of the existing ones gave me exactly what I wanted. |
| 10 | |
| 11 | ## Well what is it you wanted? |
| 12 | |
| 13 | While working on [glob](http://npm.im/glob), I found that I |
| 14 | needed a module to very efficiently manage the traversal over a |
| 15 | folder tree, such that: |
| 16 | |
| 17 | 1. No `readdir()` or `stat()` would ever be called on the same |
| 18 | file or directory more than one time. |
| 19 | 2. No `readdir()` calls would be made if we can be reasonably |
| 20 | sure that the path is not a directory. (Ie, a previous |
| 21 | `readdir()` or `stat()` covered the path, and |
| 22 | `ent.isDirectory()` is false.) |
| 23 | 3. `path.resolve()`, `dirname()`, `basename()`, and other |
| 24 | string-parsing/munging operations are be minimized. This means |
| 25 | it has to track "provisional" child nodes that may not exist |
| 26 | (and if we find that they _don't_ exist, store that |
| 27 | information as well, so we don't have to ever check again). |
| 28 | 4. The API is not limited to use as a stream/iterator/etc. There |
| 29 | are many cases where an API like node's `fs` is preferrable. |
| 30 | 5. It's more important to prevent excess syscalls than to be up |
| 31 | to date, but it should be smart enough to know what it |
| 32 | _doesn't_ know, and go get it seamlessly when requested. |
| 33 | 6. Do not blow up the JS heap allocation if operating on a |
| 34 | directory with a huge number of entries. |
| 35 | 7. Handle all the weird aspects of Windows paths, like UNC paths |
| 36 | and drive letters and wrongway slashes, so that the consumer |
| 37 | can return canonical platform-specific paths without having to |
| 38 | parse or join or do any error-prone string munging. |
| 39 | |
| 40 | ## PERFORMANCE |
| 41 | |
| 42 | JavaScript people throw around the word "blazing" a lot. I hope |
| 43 | that this module doesn't blaze anyone. But it does go very fast, |
| 44 | in the cases it's optimized for, if used properly. |
| 45 | |
| 46 | PathScurry provides ample opportunities to get extremely good |
| 47 | performance, as well as several options to trade performance for |
| 48 | convenience. |
| 49 | |
| 50 | Benchmarks can be run by executing `npm run bench`. |
| 51 | |
| 52 | As is always the case, doing more means going slower, doing less |
| 53 | means going faster, and there are trade offs between speed and |
| 54 | memory usage. |
| 55 | |
| 56 | PathScurry makes heavy use of [LRUCache](http://npm.im/lru-cache) |
| 57 | to efficiently cache whatever it can, and `Path` objects remain |
| 58 | in the graph for the lifetime of the walker, so repeated calls |
| 59 | with a single PathScurry object will be extremely fast. However, |
| 60 | adding items to a cold cache means "doing more", so in those |
| 61 | cases, we pay a price. Nothing is free, but every effort has been |
| 62 | made to reduce costs wherever possible. |
| 63 | |
| 64 | Also, note that a "cache as long as possible" approach means that |
| 65 | changes to the filesystem may not be reflected in the results of |
| 66 | repeated PathScurry operations. |
| 67 | |
| 68 | For resolving string paths, `PathScurry` ranges from 5-50 times |
| 69 | faster than `path.resolve` on repeated resolutions, but around |
| 70 | 100 to 1000 times _slower_ on the first resolution. If your |
| 71 | program is spending a lot of time resolving the _same_ paths |
| 72 | repeatedly (like, thousands or millions of times), then this can |
| 73 | be beneficial. But both implementations are pretty fast, and |
| 74 | speeding up an infrequent operation from 4µs to 400ns is not |
| 75 | going to move the needle on your app's performance. |
| 76 | |
| 77 | For walking file system directory trees, a lot depends on how |
| 78 | often a given PathScurry object will be used, and also on the |
| 79 | walk method used. |
| 80 | |
| 81 | With default settings on a folder tree of 100,000 items, |
| 82 | consisting of around a 10-to-1 ratio of normal files to |
| 83 | directories, PathScurry performs comparably to |
| 84 | [@nodelib/fs.walk](http://npm.im/@nodelib/fs.walk), which is the |
| 85 | fastest and most reliable file system walker I could find. As far |
| 86 | as I can tell, it's almost impossible to go much faster in a |
| 87 | Node.js program, just based on how fast you can push syscalls out |
| 88 | to the fs thread pool. |
| 89 | |
| 90 | On my machine, that is about 1000-1200 completed walks per second |
| 91 | for async or stream walks, and around 500-600 walks per second |
| 92 | synchronously. |
| 93 | |
| 94 | In the warm cache state, PathScurry's performance increases |
| 95 | around 4x for async `for await` iteration, 10-15x faster for |
| 96 | streams and synchronous `for of` iteration, and anywhere from 30x |
| 97 | to 80x faster for the rest. |
| 98 | |
| 99 | ``` |
| 100 | # walk 100,000 fs entries, 10/1 file/dir ratio |
| 101 | # operations / ms |
| 102 | New PathScurry object | Reuse PathScurry object |
| 103 | stream: 1112.589 | 13974.917 |
| 104 | sync stream: 492.718 | 15028.343 |
| 105 | async walk: 1095.648 | 32706.395 |
| 106 | sync walk: 527.632 | 46129.772 |
| 107 | async iter: 1288.821 | 5045.510 |
| 108 | sync iter: 498.496 | 17920.746 |
| 109 | ``` |
| 110 | |
| 111 | A hand-rolled walk calling `entry.readdir()` and recursing |
| 112 | through the entries can benefit even more from caching, with |
| 113 | greater flexibility and without the overhead of streams or |
| 114 | generators. |
| 115 | |
| 116 | The cold cache state is still limited by the costs of file system |
| 117 | operations, but with a warm cache, the only bottleneck is CPU |
| 118 | speed and VM optimizations. Of course, in that case, some care |
| 119 | must be taken to ensure that you don't lose performance as a |
| 120 | result of silly mistakes, like calling `readdir()` on entries |
| 121 | that you know are not directories. |
| 122 | |
| 123 | ``` |
| 124 | # manual recursive iteration functions |
| 125 | cold cache | warm cache |
| 126 | async: 1164.901 | 17923.320 |
| 127 | cb: 1101.127 | 40999.344 |
| 128 | zalgo: 1082.240 | 66689.936 |
| 129 | sync: 526.935 | 87097.591 |
| 130 | ``` |
| 131 | |
| 132 | In this case, the speed improves by around 10-20x in the async |
| 133 | case, 40x in the case of using `entry.readdirCB` with protections |
| 134 | against synchronous callbacks, and 50-100x with callback |
| 135 | deferrals disabled, and _several hundred times faster_ for |
| 136 | synchronous iteration. |
| 137 | |
| 138 | If you can think of a case that is not covered in these |
| 139 | benchmarks, or an implementation that performs significantly |
| 140 | better than PathScurry, please [let me |
| 141 | know](https://github.com/isaacs/path-scurry/issues). |
| 142 | |
| 143 | ## USAGE |
| 144 | |
| 145 | ```ts |
| 146 | // hybrid module, load with either method |
| 147 | import { PathScurry, Path } from 'path-scurry' |
| 148 | // or: |
| 149 | const { PathScurry, Path } = require('path-scurry') |
| 150 | |
| 151 | // very simple example, say we want to find and |
| 152 | // delete all the .DS_Store files in a given path |
| 153 | // note that the API is very similar to just a |
| 154 | // naive walk with fs.readdir() |
| 155 | import { unlink } from 'fs/promises' |
| 156 | |
| 157 | // easy way, iterate over the directory and do the thing |
| 158 | const pw = new PathScurry(process.cwd()) |
| 159 | for await (const entry of pw) { |
| 160 | if (entry.isFile() && entry.name === '.DS_Store') { |
| 161 | unlink(entry.fullpath()) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // here it is as a manual recursive method |
| 166 | const walk = async (entry: Path) => { |
| 167 | const promises: Promise<any> = [] |
| 168 | // readdir doesn't throw on non-directories, it just doesn't |
| 169 | // return any entries, to save stack trace costs. |
| 170 | // Items are returned in arbitrary unsorted order |
| 171 | for (const child of await pw.readdir(entry)) { |
| 172 | // each child is a Path object |
| 173 | if (child.name === '.DS_Store' && child.isFile()) { |
| 174 | // could also do pw.resolve(entry, child.name), |
| 175 | // just like fs.readdir walking, but .fullpath is |
| 176 | // a *slightly* more efficient shorthand. |
| 177 | promises.push(unlink(child.fullpath())) |
| 178 | } else if (child.isDirectory()) { |
| 179 | promises.push(walk(child)) |
| 180 | } |
| 181 | } |
| 182 | return Promise.all(promises) |
| 183 | } |
| 184 | |
| 185 | walk(pw.cwd).then(() => { |
| 186 | console.log('all .DS_Store files removed') |
| 187 | }) |
| 188 | |
| 189 | const pw2 = new PathScurry('/a/b/c') // pw2.cwd is the Path for /a/b/c |
| 190 | const relativeDir = pw2.cwd.resolve('../x') // Path entry for '/a/b/x' |
| 191 | const relative2 = pw2.cwd.resolve('/a/b/d/../x') // same path, same entry |
| 192 | assert.equal(relativeDir, relative2) |
| 193 | ``` |
| 194 | |
| 195 | ## API |
| 196 | |
| 197 | [Full TypeDoc API](https://isaacs.github.io/path-scurry) |
| 198 | |
| 199 | There are platform-specific classes exported, but for the most |
| 200 | part, the default `PathScurry` and `Path` exports are what you |
| 201 | most likely need, unless you are testing behavior for other |
| 202 | platforms. |
| 203 | |
| 204 | Intended public API is documented here, but the full |
| 205 | documentation does include internal types, which should not be |
| 206 | accessed directly. |
| 207 | |
| 208 | ### Interface `PathScurryOpts` |
| 209 | |
| 210 | The type of the `options` argument passed to the `PathScurry` |
| 211 | constructor. |
| 212 | |
| 213 | - `nocase`: Boolean indicating that file names should be compared |
| 214 | case-insensitively. Defaults to `true` on darwin and win32 |
| 215 | implementations, `false` elsewhere. |
| 216 | |
| 217 | **Warning** Performing case-insensitive matching on a |
| 218 | case-sensitive filesystem will result in occasionally very |
| 219 | bizarre behavior. Performing case-sensitive matching on a |
| 220 | case-insensitive filesystem may negatively impact performance. |
| 221 | |
| 222 | - `childrenCacheSize`: Number of child entries to cache, in order |
| 223 | to speed up `resolve()` and `readdir()` calls. Defaults to |
| 224 | `16 * 1024` (ie, `16384`). |
| 225 | |
| 226 | Setting it to a higher value will run the risk of JS heap |
| 227 | allocation errors on large directory trees. Setting it to `256` |
| 228 | or smaller will significantly reduce the construction time and |
| 229 | data consumption overhead, but with the downside of operations |
| 230 | being slower on large directory trees. Setting it to `0` will |
| 231 | mean that effectively no operations are cached, and this module |
| 232 | will be roughly the same speed as `fs` for file system |
| 233 | operations, and _much_ slower than `path.resolve()` for |
| 234 | repeated path resolution. |
| 235 | |
| 236 | - `fs` An object that will be used to override the default `fs` |
| 237 | methods. Any methods that are not overridden will use Node's |
| 238 | built-in implementations. |
| 239 | |
| 240 | - lstatSync |
| 241 | - readdir (callback `withFileTypes` Dirent variant, used for |
| 242 | readdirCB and most walks) |
| 243 | - readdirSync |
| 244 | - readlinkSync |
| 245 | - realpathSync |
| 246 | - promises: Object containing the following async methods: |
| 247 | - lstat |
| 248 | - readdir (Dirent variant only) |
| 249 | - readlink |
| 250 | - realpath |
| 251 | |
| 252 | ### Interface `WalkOptions` |
| 253 | |
| 254 | The options object that may be passed to all walk methods. |
| 255 | |
| 256 | - `withFileTypes`: Boolean, default true. Indicates that `Path` |
| 257 | objects should be returned. Set to `false` to get string paths |
| 258 | instead. |
| 259 | - `follow`: Boolean, default false. Attempt to read directory |
| 260 | entries from symbolic links. Otherwise, only actual directories |
| 261 | are traversed. Regardless of this setting, a given target path |
| 262 | will only ever be walked once, meaning that a symbolic link to |
| 263 | a previously traversed directory will never be followed. |
| 264 | |
| 265 | Setting this imposes a slight performance penalty, because |
| 266 | `readlink` must be called on all symbolic links encountered, in |
| 267 | order to avoid infinite cycles. |
| 268 | |
| 269 | - `filter`: Function `(entry: Path) => boolean`. If provided, |
| 270 | will prevent the inclusion of any entry for which it returns a |
| 271 | falsey value. This will not prevent directories from being |
| 272 | traversed if they do not pass the filter, though it will |
| 273 | prevent the directories themselves from being included in the |
| 274 | results. By default, if no filter is provided, then all entries |
| 275 | are included in the results. |
| 276 | - `walkFilter`: Function `(entry: Path) => boolean`. If provided, |
| 277 | will prevent the traversal of any directory (or in the case of |
| 278 | `follow:true` symbolic links to directories) for which the |
| 279 | function returns false. This will not prevent the directories |
| 280 | themselves from being included in the result set. Use `filter` |
| 281 | for that. |
| 282 | |
| 283 | Note that TypeScript return types will only be inferred properly |
| 284 | from static analysis if the `withFileTypes` option is omitted, or |
| 285 | a constant `true` or `false` value. |
| 286 | |
| 287 | ### Class `PathScurry` |
| 288 | |
| 289 | The main interface. Defaults to an appropriate class based on the |
| 290 | current platform. |
| 291 | |
| 292 | Use `PathScurryWin32`, `PathScurryDarwin`, or `PathScurryPosix` |
| 293 | if implementation-specific behavior is desired. |
| 294 | |
| 295 | All walk methods may be called with a `WalkOptions` argument to |
| 296 | walk over the object's current working directory with the |
| 297 | supplied options. |
| 298 | |
| 299 | #### `async pw.walk(entry?: string | Path | WalkOptions, opts?: WalkOptions)` |
| 300 | |
| 301 | Walk the directory tree according to the options provided, |
| 302 | resolving to an array of all entries found. |
| 303 | |
| 304 | #### `pw.walkSync(entry?: string | Path | WalkOptions, opts?: WalkOptions)` |
| 305 | |
| 306 | Walk the directory tree according to the options provided, |
| 307 | returning an array of all entries found. |
| 308 | |
| 309 | #### `pw.iterate(entry?: string | Path | WalkOptions, opts?: WalkOptions)` |
| 310 | |
| 311 | Iterate over the directory asynchronously, for use with `for |
| 312 | await of`. This is also the default async iterator method. |
| 313 | |
| 314 | #### `pw.iterateSync(entry?: string | Path | WalkOptions, opts?: WalkOptions)` |
| 315 | |
| 316 | Iterate over the directory synchronously, for use with `for of`. |
| 317 | This is also the default sync iterator method. |
| 318 | |
| 319 | #### `pw.stream(entry?: string | Path | WalkOptions, opts?: WalkOptions)` |
| 320 | |
| 321 | Return a [Minipass](http://npm.im/minipass) stream that emits |
| 322 | each entry or path string in the walk. Results are made available |
| 323 | asynchronously. |
| 324 | |
| 325 | #### `pw.streamSync(entry?: string | Path | WalkOptions, opts?: WalkOptions)` |
| 326 | |
| 327 | Return a [Minipass](http://npm.im/minipass) stream that emits |
| 328 | each entry or path string in the walk. Results are made available |
| 329 | synchronously, meaning that the walk will complete in a single |
| 330 | tick if the stream is fully consumed. |
| 331 | |
| 332 | #### `pw.cwd` |
| 333 | |
| 334 | Path object representing the current working directory for the |
| 335 | PathScurry. |
| 336 | |
| 337 | #### `pw.chdir(path: string)` |
| 338 | |
| 339 | Set the new effective current working directory for the scurry |
| 340 | object, so that `path.relative()` and `path.relativePosix()` |
| 341 | return values relative to the new cwd path. |
| 342 | |
| 343 | #### `pw.depth(path?: Path | string): number` |
| 344 | |
| 345 | Return the depth of the specified path (or the PathScurry cwd) |
| 346 | within the directory tree. |
| 347 | |
| 348 | Root entries have a depth of `0`. |
| 349 | |
| 350 | #### `pw.resolve(...paths: string[])` |
| 351 | |
| 352 | Caching `path.resolve()`. |
| 353 | |
| 354 | Significantly faster than `path.resolve()` if called repeatedly |
| 355 | with the same paths. Significantly slower otherwise, as it builds |
| 356 | out the cached Path entries. |
| 357 | |
| 358 | To get a `Path` object resolved from the `PathScurry`, use |
| 359 | `pw.cwd.resolve(path)`. Note that `Path.resolve` only takes a |
| 360 | single string argument, not multiple. |
| 361 | |
| 362 | #### `pw.resolvePosix(...paths: string[])` |
| 363 | |
| 364 | Caching `path.resolve()`, but always using posix style paths. |
| 365 | |
| 366 | This is identical to `pw.resolve(...paths)` on posix systems (ie, |
| 367 | everywhere except Windows). |
| 368 | |
| 369 | On Windows, it returns the full absolute UNC path using `/` |
| 370 | separators. Ie, instead of `'C:\\foo\\bar`, it would return |
| 371 | `//?/C:/foo/bar`. |
| 372 | |
| 373 | #### `pw.relative(path: string | Path): string` |
| 374 | |
| 375 | Return the relative path from the PathWalker cwd to the supplied |
| 376 | path string or entry. |
| 377 | |
| 378 | If the nearest common ancestor is the root, then an absolute path |
| 379 | is returned. |
| 380 | |
| 381 | #### `pw.relativePosix(path: string | Path): string` |
| 382 | |
| 383 | Return the relative path from the PathWalker cwd to the supplied |
| 384 | path string or entry, using `/` path separators. |
| 385 | |
| 386 | If the nearest common ancestor is the root, then an absolute path |
| 387 | is returned. |
| 388 | |
| 389 | On posix platforms (ie, all platforms except Windows), this is |
| 390 | identical to `pw.relative(path)`. |
| 391 | |
| 392 | On Windows systems, it returns the resulting string as a |
| 393 | `/`-delimited path. If an absolute path is returned (because the |
| 394 | target does not share a common ancestor with `pw.cwd`), then a |
| 395 | full absolute UNC path will be returned. Ie, instead of |
| 396 | `'C:\\foo\\bar`, it would return `//?/C:/foo/bar`. |
| 397 | |
| 398 | #### `pw.basename(path: string | Path): string` |
| 399 | |
| 400 | Return the basename of the provided string or Path. |
| 401 | |
| 402 | #### `pw.dirname(path: string | Path): string` |
| 403 | |
| 404 | Return the parent directory of the supplied string or Path. |
| 405 | |
| 406 | #### `async pw.readdir(dir = pw.cwd, opts = { withFileTypes: true })` |
| 407 | |
| 408 | Read the directory and resolve to an array of strings if |
| 409 | `withFileTypes` is explicitly set to `false` or Path objects |
| 410 | otherwise. |
| 411 | |
| 412 | Can be called as `pw.readdir({ withFileTypes: boolean })` as |
| 413 | well. |
| 414 | |
| 415 | Returns `[]` if no entries are found, or if any error occurs. |
| 416 | |
| 417 | Note that TypeScript return types will only be inferred properly |
| 418 | from static analysis if the `withFileTypes` option is omitted, or |
| 419 | a constant `true` or `false` value. |
| 420 | |
| 421 | #### `pw.readdirSync(dir = pw.cwd, opts = { withFileTypes: true })` |
| 422 | |
| 423 | Synchronous `pw.readdir()` |
| 424 | |
| 425 | #### `async pw.readlink(link = pw.cwd, opts = { withFileTypes: false })` |
| 426 | |
| 427 | Call `fs.readlink` on the supplied string or Path object, and |
| 428 | return the result. |
| 429 | |
| 430 | Can be called as `pw.readlink({ withFileTypes: boolean })` as |
| 431 | well. |
| 432 | |
| 433 | Returns `undefined` if any error occurs (for example, if the |
| 434 | argument is not a symbolic link), or a `Path` object if |
| 435 | `withFileTypes` is explicitly set to `true`, or a string |
| 436 | otherwise. |
| 437 | |
| 438 | Note that TypeScript return types will only be inferred properly |
| 439 | from static analysis if the `withFileTypes` option is omitted, or |
| 440 | a constant `true` or `false` value. |
| 441 | |
| 442 | #### `pw.readlinkSync(link = pw.cwd, opts = { withFileTypes: false })` |
| 443 | |
| 444 | Synchronous `pw.readlink()` |
| 445 | |
| 446 | #### `async pw.lstat(entry = pw.cwd)` |
| 447 | |
| 448 | Call `fs.lstat` on the supplied string or Path object, and fill |
| 449 | in as much information as possible, returning the updated `Path` |
| 450 | object. |
| 451 | |
| 452 | Returns `undefined` if the entry does not exist, or if any error |
| 453 | is encountered. |
| 454 | |
| 455 | Note that some `Stats` data (such as `ino`, `dev`, and `mode`) |
| 456 | will not be supplied. For those things, you'll need to call |
| 457 | `fs.lstat` yourself. |
| 458 | |
| 459 | #### `pw.lstatSync(entry = pw.cwd)` |
| 460 | |
| 461 | Synchronous `pw.lstat()` |
| 462 | |
| 463 | #### `pw.realpath(entry = pw.cwd, opts = { withFileTypes: false })` |
| 464 | |
| 465 | Call `fs.realpath` on the supplied string or Path object, and |
| 466 | return the realpath if available. |
| 467 | |
| 468 | Returns `undefined` if any error occurs. |
| 469 | |
| 470 | May be called as `pw.realpath({ withFileTypes: boolean })` to run |
| 471 | on `pw.cwd`. |
| 472 | |
| 473 | #### `pw.realpathSync(entry = pw.cwd, opts = { withFileTypes: false })` |
| 474 | |
| 475 | Synchronous `pw.realpath()` |
| 476 | |
| 477 | ### Class `Path` implements [fs.Dirent](https://nodejs.org/docs/latest/api/fs.html#class-fsdirent) |
| 478 | |
| 479 | Object representing a given path on the filesystem, which may or |
| 480 | may not exist. |
| 481 | |
| 482 | Note that the actual class in use will be either `PathWin32` or |
| 483 | `PathPosix`, depending on the implementation of `PathScurry` in |
| 484 | use. They differ in the separators used to split and join path |
| 485 | strings, and the handling of root paths. |
| 486 | |
| 487 | In `PathPosix` implementations, paths are split and joined using |
| 488 | the `'/'` character, and `'/'` is the only root path ever in use. |
| 489 | |
| 490 | In `PathWin32` implementations, paths are split using either |
| 491 | `'/'` or `'\\'` and joined using `'\\'`, and multiple roots may |
| 492 | be in use based on the drives and UNC paths encountered. UNC |
| 493 | paths such as `//?/C:/` that identify a drive letter, will be |
| 494 | treated as an alias for the same root entry as their associated |
| 495 | drive letter (in this case `'C:\\'`). |
| 496 | |
| 497 | #### `path.name` |
| 498 | |
| 499 | Name of this file system entry. |
| 500 | |
| 501 | **Important**: _always_ test the path name against any test |
| 502 | string using the `isNamed` method, and not by directly comparing |
| 503 | this string. Otherwise, unicode path strings that the system sees |
| 504 | as identical will not be properly treated as the same path, |
| 505 | leading to incorrect behavior and possible security issues. |
| 506 | |
| 507 | #### `path.isNamed(name: string): boolean` |
| 508 | |
| 509 | Return true if the path is a match for the given path name. This |
| 510 | handles case sensitivity and unicode normalization. |
| 511 | |
| 512 | Note: even on case-sensitive systems, it is **not** safe to test |
| 513 | the equality of the `.name` property to determine whether a given |
| 514 | pathname matches, due to unicode normalization mismatches. |
| 515 | |
| 516 | Always use this method instead of testing the `path.name` |
| 517 | property directly. |
| 518 | |
| 519 | #### `path.isCWD` |
| 520 | |
| 521 | Set to true if this `Path` object is the current working |
| 522 | directory of the `PathScurry` collection that contains it. |
| 523 | |
| 524 | #### `path.getType()` |
| 525 | |
| 526 | Returns the type of the Path object, `'File'`, `'Directory'`, |
| 527 | etc. |
| 528 | |
| 529 | #### `path.isType(t: type)` |
| 530 | |
| 531 | Returns true if `is{t}()` returns true. |
| 532 | |
| 533 | For example, `path.isType('Directory')` is equivalent to |
| 534 | `path.isDirectory()`. |
| 535 | |
| 536 | #### `path.depth()` |
| 537 | |
| 538 | Return the depth of the Path entry within the directory tree. |
| 539 | Root paths have a depth of `0`. |
| 540 | |
| 541 | #### `path.fullpath()` |
| 542 | |
| 543 | The fully resolved path to the entry. |
| 544 | |
| 545 | #### `path.fullpathPosix()` |
| 546 | |
| 547 | The fully resolved path to the entry, using `/` separators. |
| 548 | |
| 549 | On posix systems, this is identical to `path.fullpath()`. On |
| 550 | windows, this will return a fully resolved absolute UNC path |
| 551 | using `/` separators. Eg, instead of `'C:\\foo\\bar'`, it will |
| 552 | return `'//?/C:/foo/bar'`. |
| 553 | |
| 554 | #### `path.isFile()`, `path.isDirectory()`, etc. |
| 555 | |
| 556 | Same as the identical `fs.Dirent.isX()` methods. |
| 557 | |
| 558 | #### `path.isUnknown()` |
| 559 | |
| 560 | Returns true if the path's type is unknown. Always returns true |
| 561 | when the path is known to not exist. |
| 562 | |
| 563 | #### `path.resolve(p: string)` |
| 564 | |
| 565 | Return a `Path` object associated with the provided path string |
| 566 | as resolved from the current Path object. |
| 567 | |
| 568 | #### `path.relative(): string` |
| 569 | |
| 570 | Return the relative path from the PathWalker cwd to the supplied |
| 571 | path string or entry. |
| 572 | |
| 573 | If the nearest common ancestor is the root, then an absolute path |
| 574 | is returned. |
| 575 | |
| 576 | #### `path.relativePosix(): string` |
| 577 | |
| 578 | Return the relative path from the PathWalker cwd to the supplied |
| 579 | path string or entry, using `/` path separators. |
| 580 | |
| 581 | If the nearest common ancestor is the root, then an absolute path |
| 582 | is returned. |
| 583 | |
| 584 | On posix platforms (ie, all platforms except Windows), this is |
| 585 | identical to `pw.relative(path)`. |
| 586 | |
| 587 | On Windows systems, it returns the resulting string as a |
| 588 | `/`-delimited path. If an absolute path is returned (because the |
| 589 | target does not share a common ancestor with `pw.cwd`), then a |
| 590 | full absolute UNC path will be returned. Ie, instead of |
| 591 | `'C:\\foo\\bar`, it would return `//?/C:/foo/bar`. |
| 592 | |
| 593 | #### `async path.readdir()` |
| 594 | |
| 595 | Return an array of `Path` objects found by reading the associated |
| 596 | path entry. |
| 597 | |
| 598 | If path is not a directory, or if any error occurs, returns `[]`, |
| 599 | and marks all children as provisional and non-existent. |
| 600 | |
| 601 | #### `path.readdirSync()` |
| 602 | |
| 603 | Synchronous `path.readdir()` |
| 604 | |
| 605 | #### `async path.readlink()` |
| 606 | |
| 607 | Return the `Path` object referenced by the `path` as a symbolic |
| 608 | link. |
| 609 | |
| 610 | If the `path` is not a symbolic link, or any error occurs, |
| 611 | returns `undefined`. |
| 612 | |
| 613 | #### `path.readlinkSync()` |
| 614 | |
| 615 | Synchronous `path.readlink()` |
| 616 | |
| 617 | #### `async path.lstat()` |
| 618 | |
| 619 | Call `lstat` on the path object, and fill it in with details |
| 620 | determined. |
| 621 | |
| 622 | If path does not exist, or any other error occurs, returns |
| 623 | `undefined`, and marks the path as "unknown" type. |
| 624 | |
| 625 | #### `path.lstatSync()` |
| 626 | |
| 627 | Synchronous `path.lstat()` |
| 628 | |
| 629 | #### `async path.realpath()` |
| 630 | |
| 631 | Call `realpath` on the path, and return a Path object |
| 632 | corresponding to the result, or `undefined` if any error occurs. |
| 633 | |
| 634 | #### `path.realpathSync()` |
| 635 | |
| 636 | Synchornous `path.realpath()` |